php可以像人类一样获取页面
当我使用chrome浏览器浏览online.az网站时,一切都正常。网站正在为我开放。但是我想用curl打开这个站点。我的php代码:php可以像人类一样获取页面,php,curl,Php,Curl,当我使用chrome浏览器浏览online.az网站时,一切都正常。网站正在为我开放。但是我想用curl打开这个站点。我的php代码: // cookie // $tmpfname = tempnam("/tmp", "FOO"); $handle = fopen($tmpfname, "w"); fwrite($handle, ''); fclose($handle); // cookie // function file_get_contents_curl($url) { global $t
// cookie //
$tmpfname = tempnam("/tmp", "FOO");
$handle = fopen($tmpfname, "w");
fwrite($handle, '');
fclose($handle);
// cookie //
function file_get_contents_curl($url) {
global $tmpfname;
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$get = file_get_contents_curl('https://online.az//');
echo $get;
但有了这个代码,网站就不开放了 这是一个HTTPS站点,因此需要处理SSL。快速解决方法是添加:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
这只是绕过了证书验证
更好的修复方法是启用验证,但要做到这一点,您必须从服务器下载证书并保存它,以便PHP可以将其与服务器版本进行比较
然后您可以这样添加它:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, "PathToYour/Certificate.crt");
有关更详细的说明,请参阅本教程:在浏览器中打开会导致
403被禁止
?我从中得到的是403禁止,这是在真实的浏览器中。是的,我是一个人。在你们的国家可能是这样。但我可以在我们国家正常浏览这个网站。我想用php打开这个网站。我正在使用WAMP。因此,我确信online.az不会阻止我的网站。更可能的是,该网站配置错误。到底是什么,阿塞拜疆?为什么要投反对票?原因是什么?是啊!谢谢我会记下答案!