用PHP加载XML花费的时间太长

用PHP加载XML花费的时间太长,php,xml,Php,Xml,我正试图从一个在线XML文件中检索信息,但获取这些信息的时间太长。它甚至得到了超时错误的大部分时间 奇怪的是,当我直接在浏览器上打开时,速度很快 $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($

我正试图从一个在线XML文件中检索信息,但获取这些信息的时间太长。它甚至得到了超时错误的大部分时间

奇怪的是,当我直接在浏览器上打开时,速度很快

$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);

这是因为它们会根据您使用的浏览器进行阻止。 试试这个:

$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);

顺便说一句,在该文件中,您可以看到“禁止重新分发”,因此您可能希望查找一些免版税的天气数据源。

以上代码对我来说非常适用。尝试从其他位置读取另一个xml文件(较小)


看起来像是防火墙问题

一旦您用cURL发送了faux user agent头,那么最好将XML缓存到服务器上。对于天气,也许一个小时是一个好时机(玩这个,如果RSS更新更频繁,你可能想尝试15分钟)

一旦它在本地保存到您的服务器上,读取它和解析XML就会快得多

也请记住,RSS并不表示禁止重新分发。IIRC有一些免费的在线天气RSS源,所以也许你应该试试另一个