0

I receive a $_POST array from a system after a certain action has been performed on a database. I've tried accesing the array and saving it as an simple_xml object. So far I have not succeeded. The array which is being sent looks like this.

Array
(
     [xml] => <?xml version=\"1.0\"?>
     <callback><profile><id>313009</id><fields><email>[email protected]</email><nieuwsbrief>8</nieuwsbrief></fields><interests></interests></profile><action>update</action><parameters><nieuwsbrief>8</nieuwsbrief></parameters></callback>
)

I've tried storing this as a simple_xml object like this.

if($xml_test = simplexml_load_string($_POST['xml']))
{
    $xml_test->saveXML('test.xml');
}

I can save a different kind of .xml file so I'm sure the permissions are set right. As for the rest I'm not that familiar with formatting and parsing .xml files so I hope anyone could help a hand with this.

My desired outcome is that I can access the newly made simple_xml object and access the values and fields.

2
  • Trying a var_dump() on the array seeing what else is in it. had it on print_r() Commented Apr 16, 2014 at 16:04
  • 1
    What errors are you getting? Set error_reporting(E_ALL); at the top. Commented Apr 16, 2014 at 16:06

2 Answers 2

1

Need to stripslashes():

$string = stripslashes($_POST['xml']);

if($xml_test = simplexml_load_string($string))
{
    $xml_test->saveXML('test.xml');
}

It would be better to turn off magic_quotes_gpc in php.ini:

Sign up to request clarification or add additional context in comments.

2 Comments

Well spot. I hadn't noticed that the input is not valid XML.
Thank you that did it. Was it the <?xml version=\"1.0\"?> part which made it a non valid xml string?
0

saveXML() will write out the XML into the file. If you want to access the XML elements you will need to do so by doing something like

    $action = $xml_test->action;
    echo $action;

You can try printing out the simplexml object so you can see its structure of it by doing

    print_r($xml_test);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.