用PHP封装一个强大且通用的cURL方法

用PHP封装一个强大且通用的cURL方法。
用PHP封装一个强大且通用的cURL方法。
用PHP封装一个强大且通用的cURL方法。
用PHP封装一个强大且通用的cURL方法。

/**
 * @function 强大且通用的cURL请求库
 * @param    $url            string        路径       如:https://example.com/a/b?key=val&k=>v
 * @param    $method         string        请求方式   如:get、post、put、delete、patch、options
 * @param    $payload        array|string  荷载       如:['foo' => 'bar', 'upload_file' => new CURLFile(file_path)]或json{"foo":"bar"}
 * @param    $request_header array         请求头     如:['Content-Type' => 'json', 'Set-Cookie' => 'foo']
 * @param    $time_out       int           超时秒数   如:10,(单位:秒)
 * @return   array [bool 请求是否成功, string 错误内容, [int http状态码, array 响应头, string 响应主体内容]];
 */
function curl($url, $method = 'GET', $payload = [], $request_header = [], $time_out = 10) {
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL, $url);

    $method = strtoupper($method);
    if ($method == 'POST') {
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
    } else if ($method == 'PUT') {
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
    } else if ($method == 'DELETE') {
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
    } else if ($method == 'PATCH') {
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
    } else if ($method == 'OPTIONS') {
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'OPTIONS');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
    } else if($method == 'HEAD') {
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
    } else {
        curl_setopt($curl, CURLOPT_HTTPGET, true);
    }

    if ((parse_url($url)['scheme'] ?? '') == 'https') {
        //防止对12306类似的使用自家的ssl证书,造成的请求失败
        //禁止验证对等证书
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        //禁止验证主机证书
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }

    if ($time_out) {
        curl_setopt($curl, CURLOPT_TIMEOUT, $time_out);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $time_out);
    }

    if ($request_header) {
        //追加请求头 配置curl内容
        curl_setopt($curl, CURLOPT_HTTPHEADER, array_map(function ($key, $value) {return $key . ': ' . $value;}, array_keys($request_header), $request_header));
    }

    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLINFO_HEADER_OUT, true);
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    $response    = curl_exec($curl);
    $curl_info   = curl_getinfo($curl);
    $body        = substr($response, $curl_info['header_size']);
    $header_str  = trim(substr($response, 0, $curl_info['header_size']));
    $header = [];
    if($header_str) {
        $header_arr  = explode("\r\n", $header_str);
        foreach($header_arr as $every_header) {
            $header_temp = explode(': ', $every_header, 2);
            if(count($header_temp) == 2) {
                $header[$header_temp[0]] = $header_temp[1];
            }
        }
    }
    curl_close($curl);

    if (curl_errno($curl)) {
        return ['status' => false, 'msg'  => curl_error($curl), 'data' => ['http_code' => $curl_info['http_code'], 'body' => '', 'header' => [], 'info' => $curl_info]];
    }
    return ['status' => true, 'msg'  => '', 'data' => ['http_code' => $curl_info['http_code'], 'body' => $body, 'header' => $header, 'info' => $curl_info]];
}
posted @ 2023-08-09 11:51  小松聊PHP进阶  阅读(418)  评论(0编辑  收藏  举报