php 像人一样卷曲获取页面

php curl get page like human

当我使用 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 $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");

更详细的解释,请看这个教程:
Using cURL in PHP to access HTTPS (SSL/TLS) protected sites