tags:

views:

107

answers:

3

Hello, I'm trying to retrieve information from an online XML file and it takes too long to get that information. It even get most of the times timeout error.

The strange part is that when i open the link directly on the browser is fast.

$xmlobj = simplexml_load_file("http://apple.accuweather.com/adcbin/apple/Apple_Weather_Data.asp?zipcode=EUR;PT;PO019;REGUA");
print header("Content-type: text/plain");
print_r($xmlobj);
+2  A: 

That's because they're blocking depending what browser you're using. Try this:

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.6) Gecko/2009012700 SUSE/3.0.6-1.4 Firefox/3.0.6');                                                                                                                                     
curl_setopt($curl, CURLOPT_URL,'http://apple.accuweather.com/adcbin/apple/Apple_Weather_Data.asp?zipcode=EUR;PT;PO019;REGUA');             
$xmlstr = curl_exec($curl);
$xmlobj = simplexml_load_string($xmlstr);
print header("Content-type: text/plain");
print_r($xmlobj);

BTW. in the file you can see "Redistribution Prohibited", so you might want to look for some royalty-free source of weather data.

vartec
haven't noticed that... thanks for the warning.
A: 

The above code works perfectly fine for me. Try reading another xml file (small size) from a different location.

Looks like a firewall issue for me!

Shoban
A: 

Once you've sent the faux user agent headers with cURL as vartec pointed out, it might be a good idea to cache the XML to your server. For weather, maybe an hour would be a good time (play with this, if the RSS is updating more frequently, you may want to try 15 minutes).

Once it is saved locally to your server, reading it and parsing the XML will be much quicker.

Keep in mind too that the RSS does state Redistribution Prohibited. IIRC there are a few free online weather RSS feeds, so maybe you should try another one.

alex