为什么我使用curl得到错误的数据? [英] why do I get wrong data using curl?

查看:161
本文介绍了为什么我使用curl得到错误的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获取rss,出于某种原因得到错误的数据:

  $ url =http:// rss .news.yahoo.com / rss / oddlyenough; 
$ ch = curl_init($ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_HEADER,0);
$ xml = curl_exec($ ch);
curl_close($ ch);
echo htmlentities($ xml,ENT_QUOTES,UTF-8);

输出:

 <! -  rc2.ops.ch1.yahoo.com uncompressed / chunked Sun Nov 25 15:57:06 UTC 2012  - > 

如果我尝试以其他方式加载此数据,我获得正确的数据。例如,这个工作原理:

  $ xml = simplexml_load_file('http://rss.news.yahoo.com/rss/说来也怪'); 
print< ul> \\\
;
foreach($ xml-> channel-> item as $ item){
print< li> $ item-> title< / li> \\\
;
}
print< / ul>;你可以告诉我使用curl的代码有什么问题吗?




<

解决方案

您正在使用位置标记。


$ b b

添加此选项:

  curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1); 

以便具有:

  $ url =http://rss.news.yahoo.com/rss/oddlyenough; 
$ ch = curl_init($ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ ch,CURLOPT_HEADER,0);
$ xml = curl_exec($ ch);
curl_close($ ch);
echo htmlentities($ xml,ENT_QUOTES,UTF-8);



详细



代码,从Yahoo!收到的第一个答案是:

  HTTP / 1.0 301永久移动
日期: 25 Nov 2012 16:31:36 GMT
P3P:policyref =http://info.yahoo.com/w3c/p3p.xml,CP =CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi高速缓存控制:最大时间= 3600,公共
位置:http:/ / /news.yahoo.com/rss/oddlyenough
Vary:Accept-Encoding
Content-Type:text / html; charset = utf-8
年龄:1586
Content-Length:81
Via:HTTP / 1.1 rc4.ops.ch1.yahoo.com(YahooTrafficServer / 1.20.10 [cHs f])
服务器:YTS / 1.20.10

<! - rc4.ops.ch1.yahoo.com uncompressed / chunked Sun Nov 25 16:31:36 UTC 2012 - >

,它会告诉您使用新地址 http://news.yahoo.com / rss / oddlyenough



实际上,如果您直接使用新地址,原始代码地址,这是...),有点快,只有一个请求,而不是两个。


I try to get rss, I get wrong data for some reason:

$url = "http://rss.news.yahoo.com/rss/oddlyenough";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$xml = curl_exec($ch);      
curl_close($ch);
echo htmlentities($xml, ENT_QUOTES, "UTF-8");

The output:

<!-- rc2.ops.ch1.yahoo.com uncompressed/chunked Sun Nov 25 15:57:06 UTC 2012 --> 

If I try to load this data other way I get correct data. For example this one works:

$xml = simplexml_load_file('http://rss.news.yahoo.com/rss/oddlyenough');
print "<ul>\n";
foreach ($xml->channel->item as $item){
  print "<li>$item->title</li>\n";
}
print "</ul>";

Could you please tell me what's the problem with code using curl?

解决方案

You're running against a Location snag.

Add this option:

  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

so as to have:

$url = "http://rss.news.yahoo.com/rss/oddlyenough";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$xml = curl_exec($ch);      
curl_close($ch);
echo htmlentities($xml, ENT_QUOTES, "UTF-8");

Details

When you run the above code, the first answer you receive from Yahoo! is:

HTTP/1.0 301 Moved Permanently
Date: Sun, 25 Nov 2012 16:31:36 GMT
P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
Cache-Control: max-age=3600, public
Location: http://news.yahoo.com/rss/oddlyenough
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8
Age: 1586
Content-Length: 81
Via: HTTP/1.1 rc4.ops.ch1.yahoo.com (YahooTrafficServer/1.20.10 [cHs f ])
Server: YTS/1.20.10

<!-- rc4.ops.ch1.yahoo.com uncompressed/chunked Sun Nov 25 16:31:36 UTC 2012 -->

and it tells you to use the new address http://news.yahoo.com/rss/oddlyenough.

Actually, if you use directly the new address, your original code works (until they change the address again, that is...) and is a bit faster, making only one request instead of two.

这篇关于为什么我使用curl得到错误的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆