How add empty namespace to simpleXML element - php

In order to simulate XML retrieved from a SOAP web service in a local php test environment version of the web service, I wish to add an empty namespace to an element, just as supplied by the actual web service itself.
But when I add a null string namespace argument to the addChild method, it is not added. Any other value does give me a namespace, but as I said I wish to have an empty one.
So
$xml = new SimpleXMLElement('<cdhead/>');
$nako = $xml -> addChild ('nako', '000', '');
gives me
<cdhead>
<nako>000</nako>
</cdhead>
But I wish to have
<cdhead>
<nako xmlns="">000</nako>
</cdhead>
How can I achieve that?

This problem is because you are using php 5.2. Its a bug and was fixed in the PHP 5.2 series. Here you can view the Bugreport. I've tested using PHP 5.3 and your example works out of the box. This should work in any case:
<?php
$xml = new SimpleXMLElement('<cdhead/>');
$nako = $xml -> addChild ('nako', '000');
$nako->addAttribute('xmlns', '');
echo $nako->saveXml();
It adds the attribute manually if the PHP version is lower than 5.3
UPDATE I tried this with PHP 5.2.17-dotdeb too and it worked out of the box, both
addAttribute
addChild with empty string as namespace

Related

Argument value not being passed while creating class object in PHP

I am new to PHP and am facing the below issue. There are two files:
In the first PHP file I am writing the below code to create an object
$oNotas = new gcibjdnf($sFiltro, false,'apcconc.nm_apelido', $bBuscarPorChassi);
In the second file-gcibjdnf.php, I have the below code for the constructor
public function __construct($sFiltro = '', $bPaginar = false, $sOrdem= 'apcconc.nm_apelido, danfe', $bBuscarPorChassi = false) {
...}
However,
when I print $sOrdem from the gcibjdnf.php file, I am getting "apcconc.nm_apelido, danfe" as an output.
According to me it should print "apcconc.nm_apelido". But it is printing the default parameter instead of the value passed.
I am using PHP 5.6 version. Please let me know in case anyone has any idea on this.

Error when Querying Parse with PHP

I am trying to use the Parse.com SDK with PHP. I have downloaded and installed the SDK and I have successfully created a test object.
However, when trying to retrieve an object with a basic query using the sample code provided in the Parse docs and when I try to run it I get:
Fatal Error: Class 'ParseQuery' not found in /home/jameshilton/public_html/index.php on line 109
I would have guessed that it is not linking to the SDK properly but everything else is working fine.
Any ideas what I'm doing wrong?
By looking at the code of the class ParseClient in on GitHub I can see that the classes are declared in the namespace parse. So when you need an object from the Parse library you need to select the namespace. This can be done in two ways.
At the top of your file write:
use \parse;
or when instantiating the class:
$query = new \parse\ParseQuery
And remember to make sure the files are included either through an autoloader (composer?) or manually using include or require.
Regards.

A complex soap call using PHP

I've worked on this for about 4 hours and I am real close but just missing the mark – here is what xml needs to look like
<ws:Answer QuestionID="Q_CAM_ID" ws:Datatype="string">
<ws:Value>6838</ws:Value>
</ws:Answer>
Here is what I get
<ns1:Answer QuestionID="Q_CAM_ID">
<ns1:Value>6838</ns1:Value>
</ns1:Answer>
Using
$A1 = new StdClass();
$A1->QuestionID = 'Q_CAM_ID';
$A1->Value =6838;
No matter what I try I can’t get “ws:Datatype="string"” to appear. I believe the answer is the below or real similar
$A1 = new StdClass();
$A1->QuestionID = new StdClass();
$A1->QuestionID->QuestionID ='Q_CAM_ID';
$A1->QuestionID->DataType ='string';
$A1->Value =6838;
But what I keep getting is this error
Catchable fatal error Object of class stdClass could not be converted to string when the soap call is done. If anyone has a clue I would be most appreciative
The simliest way to do it is to use a WSDL to php generator as you'll only deal with PHP object that matches the requires types. Moreover, if you a good WSDL to php generator, you'll have PHP classes that are named as the elements.
You could use https://www.wsdltophp.com or https://github.com/mikaelcom/WsdlToPhp.
Maybe I'm missing something but if you need that XML why not just make it as a String, and then convert it to what you are trying to do? If you are not using a WSDL or SimpleXML, I would just do the following.
$xml =
'
<ws:Answer QuestionID="Q_CAM_ID" ws:Datatype="string">
<ws:Value>6838</ws:Value>
</ws:Answer>
';

Different Methods in PHP Classes depending on server?

I wrote a little PHP Blogscript with classes but ended in a problem. i have 2 webspaces and on 1 it works, on the other it doesn't.
i have defined a class called $system and within this class there is a function called the_querystring. it just returns an array with all the exploded querystring entries.
<?php $system->the_querystring()['variable']; ?>
my problem is - on 1 server it works fine, on my other server i have to write
<?php $system->the_querystring(['variable']); ?>
Any idea why?
According to php.net the notation
$secondElement = getArray()[1];
is allowed from PHP 5.4
Maybe your webserver has a PHP version older, something like
$querystring = $system->the_querystring();
$querystring['variable'];
should work on both.

Attribute not creating using DomDocument in PHP

I am stuck with DomDocument
This thing is working fine, no doubt -
$resource1->appendChild($dom->createAttribute('type'))
->appendChild($dom->createTextnode("webcontent"));
It is adding type="webcontent" to resource node
However when I am using this code its not adding it to it -
$resource1->appendChild($dom->createAttribute('adlcp:scormType'))
->appendChild($dom->createTextnode("sco"));
Expected to generate - adlcp:scormType="sco" <-- Not Working
However If I am creating
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <-- Working
Code -
$manifestNode->appendChild($dom->createAttribute('xmlns:xsi'))
->appendChild($dom->createTextNode("http://www.w3.org/2001/XMLSchema-instance"));
Let me know what I am doing wrong and how do I make it working
EDIT
Error -
XML Parsing Error: prefix not bound to a namespace
Googled same with the keyword - xml parsing error prefix not bound to a namespace php but not much help.
To set an attribute on a node:
$resource1->setAttribute('type', 'webcontent');
To set a namespaced attribute on a node (assuming this is the namespace represented by the "adlcp" prefix):
$resource1->setAttributeNS('http://www.adlnet.org/xsd/adlcp_rootv1p2', 'adlcp:scormType', 'sco');

Categories