Using Simplexml to Load Remote Url

Using SimpleXML to load remote URL

The results are coming back as json. Replace simplexml_load_file with json_decode and you will see a proper object.

If you want to use xml, you need to specify it in the headers. The following code will return valid xml:

$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US¤cyCode=USD&customerIpAddress=10.184.2.9&customerSessionId=&xml=<HotelListRequest><arrivalDate>01/22/2012</arrivalDate><departureDate>01/24/2012</departureDate><RoomGroup><Room><numberOfAdults>1</numberOfAdults><numberOfChildren>1</numberOfChildren><childAges>4</childAges></Room></RoomGroup><city>Amsterdam</city><countryCode>NL</countryCode><supplierCacheTolerance>MED</supplierCacheTolerance></HotelListRequest> ';

$xml = file_get_contents($url, false, $context);
$xml = simplexml_load_string($xml);
print_r($xml);
?>

Loading XML-file from URL

simplexml_load_file() takes a filename, you'd want to use simplexml_load_string(file_get_contents($completeurl)) instead, or simplexml_load_file($completeurl)

Load multiple URLs using SimpleXML

You can create a loop that deals with the multiple urls...


$all_urls = array('http://url1', 'http://url2', 'http://url3');
foreach ($all_urls as $url) {
$xml = simplexml_load_file($url);
}

Parsing XML from URL using SimpleXML

I think you just miss a tag which is query,
try:

$value = (string) $xml->query->results->item[0]->id;
echo $value;

Loading a remote xml page with file_get_contents()

You need to have allow_url_fopen set in your server for this to work.

If you don´t, then you can use this function as a replacement:

<?php
function curl_get_file_contents($URL)
{
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
curl_close($c);

if ($contents) return $contents;
else return FALSE;
}
?>

Borrowed from here.



Related Topics



Leave a reply



Submit