user1403175 user1403175 - 1 year ago 59
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

Here is my code

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

Could anyone please tell me how to read xml data?


Answer Source

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


$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

$xml = simplexml_load_string($data);


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.