【发布时间】:2012-07-22 05:06:20
【问题描述】:
我正在尝试打开网站的主页,并使用 curl 和 php 从它的 html 标记中提取标题和描述,我在一定程度上成功地做到了这一点,但是我无法打开许多网站。我的代码在这里:
function curl_download($Url){
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
// $url is any url
$source=curl_download($url);
$d=new DOMDocument();
$d->loadHTML($source);
$title=$d->getElementsByTagName("title")->item(0)->textContent)
$domx = new DOMXPath($d);
$desc=$domx->query("//meta[@name='description']")->item(0);
$description=$desc->getAttribute('content');
?>
此代码适用于大多数网站,但有很多网站甚至无法打开。可能是什么原因?
当我尝试使用get_headers 函数获取这些网站的标头时,它工作正常,但没有使用 curl 打开这些标头。其中两个网站是blogger.com 和live.com。
【问题讨论】:
-
会发生什么?有什么错误吗?
-
只是猜测......他们是否可能阻止 curl(因为 curl 尊重
robots.txt,或者服务器端对 curl 的请求标头皱眉)? -
没有错误,什么都不返回,我试过直接用file_get_contents函数打开,结果一样,打不开。
-
curl 的反应好像网站不存在..
-
我认为这些网站需要身份验证才能访问