I am sending a XML to a webserver using PHP, and the server returns an XML response. I need help parsing values from the response! I have read the SimpleXML instructions, but my understanding is that all those are only for when XML data is already created, but needs to be parsed. This is my PHP script so far:
<?php
$ch = curl_init("http://api.online-convert.com/queue-insert");
$count = $_POST['count'];
$file = "testdoc2.txt";
$fh = fopen( $file, 'w' );
$carriageReturn = "\n";
fwrite( $fh, $count );
fclose( $fh );
$request["queue"] = file_get_contents($count);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
curl_close ($ch);
echo $response;
$xmlstr = simplexml_load_string($response);
$file = "testdoc.txt";
$fh = fopen( $file, 'w' );
$carriageReturn = "\n";
fwrite( $fh, $xmlstr);
fclose( $fh );
?>
The XML response that needs to be parsed is this:
<?xml version="1.0" encoding="utf-8"?>
<queue-answer>
<status>
<code>0</code>
<message>Successfully inserted job into queue.</message>
</status>
<params>
<downloadUrl>http://www.online-convert.com/result/35f6ddbcc2ca58e7e98addc7c2efd6eb</downloadUrl>
<hash>35f6ddbcc2ca58e7e98addc7c2efd6eb</hash>
</params>
</queue-answer>
I just need to extract the value and show it to the user. I am an iPhone developer.
You first have to create the request xml document and send it to the server, e.g. (without error handling et al and without testing because of missing api key):
then you need to parse the response