Hello, there was a problem getting some data from XML. With XML in terms of PHP has not worked yet, only with JSON. I tried XML-> JSON first, but it turned out porridge.

In general, we have the following object:

SimpleXMLElement Object ( [id] => 2162624 [inv] => 0 [amount] => 0.00 [type_curr] => WMZ [sign] => 5cdd60c2316994c120fea6 [email] => тест 1 [options] => SimpleXMLElement Object ( [option] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 15885 [type] => text ) ) ) ) 

From it you need to get [type] => text. But I managed to get only to the option, then there is an array in the name (?):

 SimpleXMLElement Object ( [@attributes] => Array ( [id] => 15885 [type] => text ) ) 

And it does not work out. The code itself (in the first line the XML request itself):

 $xml = '<request><id>2162624</id><inv>0</inv><amount>0.00</amount><type_curr>WMZ</type_curr><sign>5cdd60c2316994c120fea6</sign><email>тест 1</email><options><option id="15885" type="text"><![CDATA[тест 15885]]></option></options></request>'; $xml = simplexml_load_string($xml); print_r($xml->options->option); 

What is the problem, and what does the array do there? Can I somehow get the data I need?

PS I cannot change the XML structure, I get it from the service, here I have only an example based on one of the requests.

  • Pure thoughts are heard. If you can get this xml file manually, you can open it using a text editor and find out what permanent errors it contains. Then you can get this xml in PHP, for example, using the file_get_contents function, correct those errors in it and save it. And then you get the data you need from this corrected xml. - Hasanagha Aliyev

1 answer 1

If xml were not valid, then simplexml_load_string would return false. You need to get the object attributes :

 $attributes = $xml->options->option->attributes(); echo (string)$attributes['id']; echo (string)$attributes['type']; 

or

 $type = (string)$xml->options->option->attributes()->type; echo $type;