首页 > 建站教程 > PHP教程 >  PHP curl_exec返回false,PHP跳过SSL验证正文

PHP curl_exec返回false,PHP跳过SSL验证

我爱模板网在使用PHP做个推的推送时,调用个推的鉴权接口的时候发现curl_exec返回值为false,代码如下:
// 个推鉴权
public function doAuth($cid){
    $curl = curl_init();
    $headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
    curl_setopt($curl, CURLOPT_URL, PUSH_URL.APPID.'/auth');
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);  //将接口返回内容放到变量中而不是直接输出
    curl_setopt($curl, CURLOPT_POST, 1); // post传参
    $timestamp = $this->msectime(); // 毫秒
    $str = APPKEY.$timestamp.MASTERSECRET;
    $fields = [
        'sign' => hash("sha256", $str),
        'timestamp' => $timestamp,
        'appkey' => APPKEY
    ];
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );  //设置请求头
    // curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    $res = curl_exec($curl);
    curl_close($curl);
    $res = json_decode($res);
    //对返回的错误信息进行自定义
    if($res->code == 0){
        $user = new UserModel;
        $user->token = $res->data->token;
        $user->save();
        
        // 鉴权成功,进行推送
        $this -> doPush($cid, $res->data->token);
    }else{
        $this->return_msg(400,'鉴权失败!'.$res->msg);
    }
}
执行之后输出的结果:


在Post中添加了curl_error($curl)发现错误“SSL certificate problem: unable to get local issuer certificate”:


SSL验证出了问题,跳过SSL证书验证就好了,在Post中添加:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);