PHP里curl对https的证书配置默认是服务器端要求验证的,如果服务器端没有配置证书验证,则无法请求https路径。
如果为了简便使用不需要配置https证书的话,配置curl时将以下两项设置为false即可
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //这个是重点。
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 这个可以不用
https请求,非http请求,可能有人在各个地方看到过HTTPS请求需要加几行代码绕过SSL证书的检查等方式来成功请求到资源,但是这里好像并不需要,原因是什么?
CURLOPT_SSL_VERIFYPEER // verify the peer's SSL certificate
CURLOPT_SSL_VERIFYHOST // verify the certificate's name against host
They both default to true in Curl, and shouldn't be disabled unless you've got a good reason. Disabling them is generally only needed if you're sending requests to servers with invalid or self-signed certificates, which is only usually an issue in development. Any publicly-facing site should be presenting a valid certificate, and by disabling these options you're potentially opening yourself up to security issues.
即,除非用了非法或者自制的证书,这大多数出现在开发环境中,你才将这两行设置为false以避开ssl证书检查,否者不需要这么做,这么做是不安全的做法。
/**
* @param array $data 需要POST的参数
*/
protected function jsonPost($data){
$requestBody = json_encode($data);
$headers = [
'Content-Type: application/json',
'Content-Length: ' . strlen($requestBody)
];
$url = "https://yoururl";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
$result = curl_exec($ch);
$data = json_decode($result,true);
if(!$data){
Log::record("钱包API接口返回解析错误".$result,Log::INFO);
// 返回的原始数据解析错误
throw new MallException(json_last_error().": ".json_last_error_msg());
}
return $data;
}
PHP使用CURL详解 https://www.cnblogs.com/lxj0205/p/9360826.html
PHP中使用CURL之php curl详细解析和常见大坑 https://www.php.cn/php-weizijiaocheng-393305.html