php curl获取不到https页面内容的解决方法
用php CURL获取不到https页面内容多半原因是因为没有关闭ssl验证,只需要关闭即可,加上以下两行代码:
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false,
完整代码示例:
// 创建一个新cURL资源 $ch = curl_init(); // 设置URL和相应的选项 curl_setopt_array($ch, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => "https://www.google.com", CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', CURLOPT_HEADER => false, CURLOPT_SSL_VERIFYPEER => false, #关闭ssl证书验证 CURLOPT_SSL_VERIFYHOST => false, )); // 抓取URL并把它传递给浏览器 $data = curl_exec($ch); echo $data; //关闭cURL资源,并且释放系统资源 curl_close($data);
如果你的参数没有写入数组中,你可以这么写:
// 创建一个新cURL资源 $ch = curl_init(); // 设置URL和相应的选项 curl_setopt($ch, CURLOPT_URL, "https://www.google.com"); curl_setopt(ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt(ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'); curl_setopt($ch, CURLOPT_HEADER, false); #控制是否返回请求头信息 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); #关闭ssl证书验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 抓取URL并把它传递给浏览器 $data = curl_exec($ch); echo $data; //关闭cURL资源,并且释放系统资源 curl_close($ch);