Home  >  Article  >  Backend Development  >  How to use post method in php curl

How to use post method in php curl

藏色散人
藏色散人Original
2021-06-18 09:27:383570browse

php curl uses the post method: first start a CURL session; then check the source of the authentication certificate; then check whether the SSL encryption algorithm exists from the certificate; and finally request the https protocol interface in POST mode.

How to use post method in php curl

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to use the post method with php curl?

PHP: CURL requests the HTTPS/http protocol interface api in GET and POST modes respectively

  • curl requests the https protocol interface in GET mode

  function curl_get_https($url){
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
        $tmpInfo = curl_exec($curl); //返回api的json对象
        //关闭URL请求
        curl_close($curl);
        return $tmpInfo;    //返回json对象
    }
  • curl requests https protocol interface in POST method

    function curl_post_https($url,$data){ // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);              // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);              // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据,json格式
    }
  • general encapsulation Interface

/**
* CURL GET || post请求
* @desc: GET与post都通用
* @author: Sindsun
* @email: 2361313833@qq.com
* @date: 2019年4月24日上午10:54:31
* @param: $url 请求的地址
*       $isPostRequest 默认true是GET请求,否则是POST请求
*       $data array  请求的参数
*       $certParam  array  ['cert_path']    ['key_path']
* @return:
*/
function curl_http($url, $isPostRequest=false, $data=[], $header=[], $certParam=[]){ // 模拟提交数据函数
    $curlObj = curl_init(); // 启动一个CURL会话
    //如果是POST请求
    if( $isPostRequest ){
        curl_setopt($curlObj, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curlObj, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
    }else{  //get请求检查是否拼接了参数,如果没有,检查$data是否有参数,有参数就进行拼接操作
        $getParamStr = '';
        if(!empty($data) && is_array($data)){
            $tmpArr = [];
            foreach ($data as $k=>$v){
                $tmpArr[] = $k . '=' . $v;
            }
            $getParamStr = implode('&', $tmpArr);
        }
        //检查链接中是否有参数
        $url .= strpos($url, '?') !== false ? '&' . $getParamStr : '?' . $getParamStr;
    }
    curl_setopt($curlObj, CURLOPT_URL, $url); // 要访问的地址
    //检查链接是否https请求
    if(strpos($url, 'https') !== false){
        //设置证书
        if( !empty($certParam) && isset($certParam['cert_path']) && isset($certParam['key_path']) ){
            curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
            curl_setopt($curlObj, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
            //设置证书
            //使用证书:cert 与 key 分别属于两个.pem文件
            curl_setopt($curlObj, CURLOPT_SSLCERTTYPE,'PEM');
            curl_setopt($curlObj, CURLOPT_SSLCERT, $certParam['cert_path']);
            curl_setopt($curlObj, CURLOPT_SSLKEYTYPE,'PEM');
            curl_setopt($curlObj, CURLOPT_SSLKEY, $certParam['key_path']);
        }else{
            curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
            curl_setopt($curlObj, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        }
    }
    // 模拟用户使用的浏览器
    if(isset($_SERVER['HTTP_USER_AGENT'])){
        curl_setopt($curlObj, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    }
    curl_setopt($curlObj, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
    curl_setopt($curlObj, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    curl_setopt($curlObj, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    curl_setopt($curlObj, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curlObj, CURLOPT_HTTPHEADER, $header);   //设置头部
    curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    $result = curl_exec($curlObj); // 执行操作
    if ( curl_errno($curlObj) ) {
        $result = 'error: '.curl_error($curlObj);//捕抓异常
    }
    curl_close($curlObj); // 关闭CURL会话
    return $result; // 返回数据,json格式
}

Description: The premise is to turn on the curl switch of php and the ssl_module of the server, otherwise it cannot be used normally.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use post method in php curl. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn