我编写了一个简单的 PHP 脚本来使用 CURL 获取网站的源代码:
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/curl/cacert.pem");
$data = curl_exec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($data === false)
return false;
return array("content" => $data, "url" => $url, "httpcode" => $httpcode);
}
$data = file_get_contents_curl("https://www.facebook.com");
print_r($data);
在此示例中,我正在获取 facebook 源,但它获取与不受支持的浏览器相关的页面。事实上,重定向后的最终 URL 是这样的:http: //www.facebook.com/unsupportedbrowser
有什么问题?