开发者

He!p with SimpleXML for $response in PHP

开发者 https://www.devze.com 2023-04-08 09:03 出处:网络
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

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. T开发者_开发百科his 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):

$request = new SimpleXMLElement('<queue><apiKey/><targetType/><targetMethod/><testMode/><sourceUrl/></queue>');
$request->apiKey = 'abcdefghijklmnopqrstuvwx';
$request->targetType = 'image';
$request->targetMethod = 'convert-to-jpg';
$request->testMode = 'false';
$request->sourceUrl = 'http://whatever';
$ch = curl_init("http://api.online-convert.com/queue-insert");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('queue'=>$request->asXml()));
$response = curl_exec($ch);
// TODO: might want to test the response code here, see curl_get_info/CURLINFO_HTTP_CODE
curl_close ($ch);

then you need to parse the response

$queueAnswer = simplexml_load_string($response);
echo 'status code:', $queueAnswer->status->code;
// in case of success you should be able to access the values via
echo ' url:', queueAnswer->params->downloadUrl;
echo ' hash:', queueAnswer->params->hash;


You can parse the XML file using this

$xml=simplexml_load_string($response);
foreach($xml->children() AS $child)
{
     echo $child->getName().”:”.$child.”<br/>”;
}

You can put an if condition and extract the required message only.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号