当前位置: 首页 > 工具软件 > simple curl > 使用案例 >

PHP curl https

方风华
2023-12-01

urlencode


function curl_post_request_with_form($url, array $content, $crt_path, $key_path)
{
   $curl = curl_init(); // 启动一个CURL会话
     curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
     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, http_build_query($content)); // Post提交的数据包
     curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
     curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
     curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded; charset=utf-8'));

     // HTTPS
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
     curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
     curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
     curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
     curl_setopt($curl, CURLOPT_SSLCERT, $crt_path);
     curl_setopt($curl, CURLOPT_SSLKEY, $key_path);
     $ret = curl_exec($curl);
     $err = curl_error($curl);
     if (is_array($ret)) {
         $result = $ret;
     } else {
         $result = json_decode(trim(substr($ret, strpos($ret, "\r\n\r\n")), "\r\n"), TRUE);
     }

     MWlog::debug($result, $url);

     if (FALSE == $result || !empty($err)) {
         $errno = curl_errno($curl);
         $info = curl_getinfo($curl);
         curl_close($curl);
		return false;
     }
     curl_close($curl);

     return $result;
 }

get

function curl_get_request($full_url, $crt_path, $key_path)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $full_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 6); // 调整指定数量的超时秒数
    curl_setopt($ch, CURLOPT_TIMEOUT, 12);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8']);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
    curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
    curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
    curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
    curl_setopt($ch, CURLOPT_SSLCERT, $cert_path);
    curl_setopt($ch, CURLOPT_SSLKEY, $key_path);
    $ret = curl_exec($ch);
    $err = curl_error($ch);

    if (is_array($ret)) {
        $result = $ret;
    } else {
        $result = json_decode($ret, TRUE);
    }

    if (FALSE === $ret || !empty($err)) {
        $errno = curl_errno($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
		return false;
    }

    curl_close($ch);

    return $result;
}
    

json

function curl_post_request($url, $params, $crt_path, $key_path)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json; charset=utf-8"]);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    // HTTPS
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
    curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
    curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
    curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
    curl_setopt($ch, CURLOPT_SSLCERT, $crt_path);
    curl_setopt($ch, CURLOPT_SSLKEY, $key_path);
    $ret = curl_exec($ch);
    $err = curl_error($ch);

    if (is_array($ret)) {
        $result = $ret;
    } else {
        $pos = strrpos($ret, "\r\n\r\n");
        $result = json_decode(trim(substr($ret, $pos)),TRUE);
    }

    if (FALSE === $ret || !empty($err)) {
        $errno = curl_errno($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        return false;
    }
    curl_close($ch);
    return $result;
}
 类似资料:

相关阅读

相关文章

相关问答