self signed certificate in certificate chain问题解决

By | 2017年10月23日

在使用curl请求https链接时,会返回如下错误:

Curl ErrorSSL certificate problem: self signed certificate in certificate chain

php代码如下:

<?php
error_reporting(E_ALL);
ini_set("display_errors",1);
function send_get($urlstring)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $urlstring);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($ch);
	if(!$result){
		echo 'Curl Error'.curl_error($ch);
	}
	curl_close($ch);
	return $result;
}
$url = "https://api.telegram.org/bot3213000:xxxxxxxxxx/getMe";
echo send_get($url);
?>

这里访问的是telegram的api接口,域名是https的。

解决方法是添加如下代码:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);