I have this HTTP Post function in php which sends a form to a competition webservice, the response is an XML which gives further details if the entry was successful or not, how can I parse the results to capture the status field and give further feedback to the user?
Here is the code used:
<?php
$url = "...webserviceurl...";
$body = "userName=".$_POST["username"]."&password=".........;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form- urlencoded"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
//curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close ($ch);
print $result;
?>
Thanks
You can use the PHP DOMDocument library and DOMXpath libraries to run xpath queries on the XML:
$dom = new DOMDocument();
$dom->loadXML($result);
$xpath = new DOMXpath($dom);
echo $xpath->evaluate('string(//Error)'); // echo contents of <Error> ... </Error> node
If the response has an XML namespace that is unusual (like some APIs tend to do), you have to register the namespace and prefix all queries with it.
$xpath->registerNamespace('ns', 'https://namespace.org/namespace');
echo $xpath->evaluate('string(//ns:Error)');
There are a few options built into PHP. Try this one: simplexml_load_string
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.