每次獲取https的文章的時候,老是獲取不到,發現需要加這個東西,記錄一下。
添加下面的即可:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
完整的獲取如下:
function curl_string($url, $user_agent, $proxy, $referer_url, $cookie)
{
$header[] = ‘Cookie: ‘ . $cookie;
$ch = curl_init();
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//設置代理
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//偽裝header
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
//設置頭部
curl_setopt($ch, CURLOPT_REFERER, $referer_url);
//設置來源
curl_setopt($ch, CURLOPT_ENCODING, “gzip”);
// 編碼壓縮
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//是否採集301、302之後的頁面
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //這個需要加
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //這個需要加
//輸出頭部
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
發布者:彬彬筆記,轉載請註明出處:https://www.binbinbiji.com/zh-hant/php/2165.html