So i’m working on a php script which passes information to a server using Curl and the POST method and then takes back information from that server in the form of XML.I then want to take this information and parse it into a readable format for the user.
I grab the information using curl and load it into a variable ($result) which i then want to load into simpleXML and store in the variable $routeinfo. However when I perform a var_dump($routeinfo) there doesn’t appear to be any data stored. I’ve confirmed that there is data present in $result by echoing it.
I also figured it may have been due to the XML not showing up in the browser when I echoed $result(but it is there if I check the source). Just to check though I used htmlentities but that causes a lot of errors because < is interpreted as it’s html equivalent. Anyway I’ve reached an impass and have logged quite a few hours implementing different suggestions to no avail. My code is below and I would appreciate any help that can be rendered.
<?php
//retrieve form data in a variable
$input = $_POST['stopNo'] ;
//declare other variables needed
$api_key = 'xxxkey' ;
$api_id = 12345 ;
$url = 'url to send data to' ;
$querystring = "appID=". $api_id . "&apiKey=" . $api_key . "&stopNo" . $input ;
$data = urlencode($url) ;
//curl using
$ch = curl_init() ;
//set up curl
curl_setopt($ch, CURLOPT_URL, 'urlsendingdatato');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'appID='.urlencode($api_id).'&apiKey=' .urlencode($api_key) . '&stopNo=' .urlencode($input) );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0 ) ;
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8") ;
curl_setopt($ch, CURLOPT_BINARYTRANSFER , TRUE) ;
//execute and gather info
$result = curl_exec($ch) ;
//close connection
curl_close($ch) ;
//format data
//ent at the moment serves no purpose doesn't render the information correctly
$ent = htmlentities($result);
$routeinfo = new SimpleXMLElement($result) ;
//print data
echo "<br> $result <br>" ;
var_dump($routeinfo) ;
?>
It seems that the problem is arising when the simpleXML object is created.
So you can try creating a new simpleXMLElement object with a well defined XML file. If that works that means the fetched xml file is not of proper format.
This works for me!
But generally if there is an xml parsing issue, simpleXMLElement throws a error . Did it return any error in your case? or maybe you have switched off error reporting (error_reporting(0); ).