PHP之curl实现http与https请求的(转)

http get请求:

    function httpGet($url){  
        $curl = curl_init();  
        curl_setopt($curl, CURLOPT_URL, $url);  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
        $res=json_decode(curl_exec($curl),true);  
        curl_close($curl);  
        return $res;  
    }  

http post请求:

    function httpPost($url,$post_data){   
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $url);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
        // post数据  
        curl_setopt($ch, CURLOPT_POST, 1);  
        // post的变量  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
        $output=json_decode(curl_exec($ch),true);  
        curl_close($ch);  
        return $data;  
    }  

https get请求:

    function httpsGet($url){  
        $curl = curl_init();  
        curl_setopt($curl, CURLOPT_URL, $url);  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// https请求不验证证书和hosts  
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  
        $res=json_decode(curl_exec($curl),true);  
        curl_close($curl);  
        return $res;  
    }

http get请求:

    function httpsPost($url,$post_data){  
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $url);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
        // post数据  
        curl_setopt($ch, CURLOPT_POST, 1);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  
        // post的变量  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
        $output=json_decode(curl_exec($ch),true);  
        curl_close($ch);  
        return $output;  
    }  
curl_init// 启动一个CURL会话
curl_setopt$curlCURLOPT_URL$url// 要访问的地址
curl_setopt$curlCURLOPT_SSL_VERIFYPEER0// 对认证证书来源的检查
curl_setopt$curlCURLOPT_SSL_VERIFYHOST1// 从证书中检查SSL加密算法是否存在
curl_setopt$curlCURLOPT_USERAGENT$_SERVER'HTTP_USER_AGENT'// 模拟用户使用的浏览器
curl_setopt$curlCURLOPT_FOLLOWLOCATION1// 使用自动跳转
curl_setopt$curlCURLOPT_AUTOREFERER1// 自动设置Referer
curl_setopt$curlCURLOPT_POST1// 发送一个常规的Post请求
curl_setopt$curlCURLOPT_POSTFIELDS$post// Post提交的数据包
curl_setopt$curlCURLOPT_TIMEOUT30// 设置超时限制防止死循环
curl_setopt$curlCURLOPT_HEADER0// 显示返回的Header区域内容
curl_setopt$curlCURLOPT_RETURNTRANSFER1// 获取的信息以文件流的形式返回
curl_exec$curl// 执行操作
if curl_errno$curlecho 'Errno'curl_error$curl//捕抓异常
curl_close$curl// 关闭CURL会话
return // 返回数据,json格式
原文地址:https://www.cnblogs.com/The-second/p/8386965.html