php Warning: curl_error(): 1 is not a valid cURL handle resource错误【转】

在微信开发获取acces_token过程中使用到PHPcurl采集函数遇到的警告错误:

Warning: curl_error(): 1 is not a valid cURL handle resource in xxxx line xxx

原始代码如下:

    function getWXAccessToken(){
        //1.请求url地址
        $appid = 'APPID';//私有的,不发出来了
        $appSecret = 'APPSECRET';//私有的,不发出来了
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appSecret;
        //2.初始化
        $ch = curl_init();
        //3.设置参数
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //4.调用接口
        $res = curl_exec($ch);
        //5.关闭curl
        curl_close($ch);
        if(curl_errno($ch)){
            var_dump(curl_error($ch));
        }
        $arr = json_decode($res, true);
        var_dump($arr);
    }

经过查找问题在于我在关闭curl后又再次使用了$ch,关闭一个cURL会话会释放所有资源,cURL句柄$ch也会被释放,所以后面再使用$ch时会报错。将curl_close($ch)函数放在最后就好了。
 
原文链接:https://beltxman.com/1425.html

原文地址:https://www.cnblogs.com/KillBugMe/p/13220120.html