下面是从google新闻中获取提要的代码。有一段时间很好。如果我们试图在一小段时间内调用这个函数超过2或3,它会显示一个错误。
$url="http://news.google.com/news?q=Stella+Artois&hl=en&um=1&ie=UTF-8&output=rss";
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($data, 0, $header_size);
$body = substr($data, $header_size);
if(curl_error($ch)){
echo 'error:' . curl_error($ch);
}
curl_close($ch);
错误:连接到news.google.com端口80失败:连接被拒绝
我知道原因是,谷歌会在几次请求后阻止访问。有什么可以做的吗?我正在使用linux服务器。
发布于 2017-10-25 22:35:35
是的,谷歌将在反复尝试之后阻止,甚至可能会完全回应卷发请求。您应该使用API - https://newsapi.org/google-news-api。
发布于 2017-10-25 23:23:52
在尝试了许多选项之后,我找到了另一个api,它现在运行得很好,rss2json.com。
API调用:url=http%3A%2F%2Fnews.google.com%2Fnews%3Fq%3Dyahoo%26hl%3Den%26um%3D1%26ie%3DUTF-8%26output%3Drss
https://stackoverflow.com/questions/46947284
复制