Encapsulated php cUrl request function

不言
Release: 2023-03-25 13:16:02
Original
2489 people have browsed it

This article mainly introduces the encapsulated php cUrl request function, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

After turning on debugging mode

Encapsulated php cUrl request function

Encapsulated php cUrl request function

##Usage:

$params = [
    'name'=>'name'
    ];
$url = '';
httpRequest($url,'POST',$params,[],true);
Copy after login
/**
 * CURL请求
 * @param $url 请求url地址
 * @param $method 请求方法 get post
 * @param null $postfields post数据数组
 * @param array $headers 请求header信息
 * @param bool|false $debug 调试开启 默认false
 * @return mixed
 */
function httpRequest($url, $method = "GET", $postfields = null, $headers = array(), $debug = false)
{
    $method = strtoupper($method);
    $ci = curl_init();
    /* Curl settings */
    curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//版本
    curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");//在HTTP请求中包含一个"User-Agent: "头的字符串。
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
    curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
    switch ($method) {
        case "POST":
            curl_setopt($ci, CURLOPT_POST, true);//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
            if (!empty($postfields)) {
                $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
                curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
            }
            break;
        default:
            curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
            break;
    }
    $ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
    curl_setopt($ci, CURLOPT_URL, $url);//需要获取的URL地址,也可以在curl_init()函数中设置
    if ($ssl) {
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
    }
    //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
    curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
    curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ci, CURLINFO_HEADER_OUT, true);
    /*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
    $response = curl_exec($ci);
    $requestinfo = curl_getinfo($ci);
    if ($debug) {

        $html = <<<HTML
        <style>
            *{
            margin:0;
            padding:0;
            font-family: "微软雅黑 Light";
            color:#777; 
            }
            body{
            padding:10px;
            }
            h1{
            border:1px solid #CCCCCC;
            height: 40px;
            line-height: 40px;
            font-size: 20px;
            border-radius: 5px;
            text-align: center;
            background: #F5F5F5;
            }
            p.box{
                padding:10px 0;
            }
            p.line{
            height: 30px;
            line-height: 30px;
            border-bottom: 1px dashed #ccc;
            overflow: auto;
            }
            p.line:hover{
             background: #EEEEEE;;
             }
            span.title{
            display: inline-block;
            width:300px;
            height: 100%;
            }
            span.key{
            color:red;
            display: inline-block;
            width:200px;
            height: 100%;
            }
        </style>
        <h1>PostData</h1>
        <p class="box">
HTML;
        $i = 1;
        foreach ($postfields as $key => $value) {
            $html .= " <p class=\"line\"><span class=&#39;key&#39;>{$i}</span><span class=\"title\">{$key}:</span> <span>{$value}</span></p>";
            $i++;
        }
        $html .= <<<HTML
        </p>
        <h1>Info</h1>
            <p class="box">
HTML;
        $i = 1;
        foreach ($requestinfo as $kye => $value) {
            if (is_array($value)) continue;
            $html .= " <p class=\"line\"><span class=&#39;key&#39;>{$i}</span><span class=\"title\">{$kye}:</span> <span>{$value}</span></p>";
            $i++;
        }
        $html .= <<<HTML
            </p>
        <h1>Response</h1>
        <p class="box">
            {$response}
        </p>
<p>
HTML;

        $html .= <<<HTML
</p>
HTML;
        echo $html;
    }
    curl_close($ci);
    return $response;
}
Copy after login

Related recommendations:


Repackaged PHPLib DB class (recommended for simple projects to use this class library)


The above is the detailed content of Encapsulated php cUrl request function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!