I am relatively new to PHP. I am trying to display a XML response from a restful Web Service. Below is my code snippet:
<?php
if(isset($_GET['submit'])){
$name = $_GET['company'];
$url = "http://dev.markitondemand.com/MODApis/Api/v2/Lookup/xml?input=$name";
$client = curl_init($url);
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($client);
$xml = new SimpleXMLElement($response);
curl_close($client);
foreach($xml->LookupResultList as $oEntry){
echo $oEntry->LookupResultList->LookupResult[i]->symbol . "
";
}
}
?>
I get the following error in the console:
Invalid request (Unexpected EOF)
Also $xml is blank.
In your foreach
loop you have to use directly <LookupResult>
tag, and inside the loop you don't have to call the complete three, but only the <LookupResult>
child(ren):
foreach( $xml->LookupResult as $oEntry )
{
echo $oEntry->Symbol . PHP_EOL;
}
Also, I don't know what do you exactly mean with “$xml is blank”; BTW, to print out XML from SimpleXML
you can use this syntax:
echo $xml->asXML();