【问题标题】:why do I get wrong data using curl?为什么我使用 curl 会得到错误的数据?
【发布时间】:2012-11-26 00:56:06
【问题描述】:

我尝试获取 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/oddlyenough');
print "<ul>\n";
foreach ($xml->channel->item as $item){
  print "<li>$item->title</li>\n";
}
print "</ul>";

你能告诉我使用 curl 的代码有什么问题吗?

【问题讨论】:

  • 它显示:
  • 试试echo $xml = curl_exec($ch);
  • 在这种情况下它显示空白页面,如果我查看源代码我看到:

标签: php curl


【解决方案1】:

您遇到了Location 障碍。

添加此选项:

  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 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 -->

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

实际上,如果您直接使用新地址,您的原始代码可以工作(直到他们再次更改地址,即......)并且速度更快,只发出一个请求而不是两个。

【讨论】:

  • 非常感谢您的回答。
猜你喜欢
  • 2019-02-08
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 2022-11-25
  • 2018-04-08
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多