user1403175 user1403175 - 2 months ago 10
PHP Question

Read xml data from url using curl and php

I want to read XML data from a URL. I have the url as follow

http://www.arrowcast.net/fids/mco/fids.asp?sort=city&city=&number=&airline=&adi=A

Here is my code

$Url="http://www.arrowcast.net/fids/mco/fids.asp?sort=city&city=&number=&airline=&adi=A";
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $Url);
$output = curl_exec($ch);
$resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($ch);


Could anyone please tell me how to read xml data?

Thanks.....

Answer

Here is some sample code (XML parsing module may not be available on your PHP installation):

<?php

$url="http://www.arrowcast.net/fids/mco/fids.asp?sort=city&city=&number=&airline=&adi=A";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents

$data = curl_exec($ch); // execute curl request
curl_close($ch);

$xml = simplexml_load_string($data);
print_r($xml)

?>

The variable $xml now is a multi-dimensional key value array and you should easily be able to figure out how to get the elements from there.