【笔记】PHP封装的不错的一个Curl方法

【笔记】PHP封装的不错的一个Curl方法

huhuan
2022-03-28 / 0 评论 / 141 阅读 / 正在检测是否收录...

个人对于Curl还是蛮常用的,偶然发现一个封装这么完全的,记录下。

代码

<span><span><?php</span>
    <span>
    <span><span>function</span> <span>teacher_curl</span><span>($url, $paras = array<span>()</span>)</span>
    </span>{
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, <span>false</span>);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, <span>false</span>);
        <span>if</span> (@$paras[<span>'Header'</span>]) {
            $Header = $paras[<span>'Header'</span>];
        } <span>else</span> {
            $Header[] = <span>"Accept:*/*"</span>;
            $Header[] = <span>"Accept-Encoding:gzip,deflate,sdch"</span>;
            $Header[] = <span>"Accept-Language:zh-CN,zh;q=0.8"</span>;
            $Header[] = <span>"Connection:close"</span>;
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
        <span>if</span> (@$paras[<span>'ctime'</span>]) { <span>// 连接超时</span>
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras[<span>'ctime'</span>]);
        } <span>else</span> {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, <span>30</span>);
        }
        <span>if</span> (@$paras[<span>'rtime'</span>]) { <span>// 读取超时</span>
            curl_setopt($ch, CURLOPT_TIMEOUT, $paras[<span>'rtime'</span>]);
        }
        <span>if</span> (@$paras[<span>'post'</span>]) {
            curl_setopt($ch, CURLOPT_POST, <span>1</span>);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $paras[<span>'post'</span>]);
        }
        <span>if</span> (@$paras[<span>'header'</span>]) {
            curl_setopt($ch, CURLOPT_HEADER, <span>true</span>);
        }
        <span>if</span> (@$paras[<span>'cookie'</span>]) {
            curl_setopt($ch, CURLOPT_COOKIE, $paras[<span>'cookie'</span>]);
        }
        <span>if</span> (@$paras[<span>'refer'</span>]) {
            <span>if</span> ($paras[<span>'refer'</span>] == <span>1</span>) {
                curl_setopt($ch, CURLOPT_REFERER, <span>'http://m.qzone.com/infocenter?g_f='</span>);
            } <span>else</span> {
                curl_setopt($ch, CURLOPT_REFERER, $paras[<span>'refer'</span>]);
            }
        }
        <span>if</span> (@$paras[<span>'ua'</span>]) {
            curl_setopt($ch, CURLOPT_USERAGENT, $paras[<span>'ua'</span>]);
        } <span>else</span> {
            curl_setopt($ch, CURLOPT_USERAGENT, <span>"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"</span>);
        }
        <span>if</span> (@$paras[<span>'nobody'</span>]) {
            curl_setopt($ch, CURLOPT_NOBODY, <span>1</span>);
        }
        curl_setopt($ch, CURLOPT_ENCODING, <span>"gzip"</span>);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span>1</span>);
        <span>if</span> (@$paras[<span>'GetCookie'</span>]) {
            curl_setopt($ch, CURLOPT_HEADER, <span>1</span>);
            $result = curl_exec($ch);
            preg_match_all(<span>"/Set-Cookie: (.*?);/m"</span>, $result, $matches);
            $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $header = substr($result, <span>0</span>, $headerSize); <span>//状态码</span>
            $body = substr($result, $headerSize);
            $ret = [
                <span>"Cookie"</span> => $matches, <span>"body"</span> => $body, <span>"header"</span> => $header, <span>'code'</span> => curl_getinfo($ch, CURLINFO_HTTP_CODE)
            ];
            curl_close($ch);
            <span>return</span> $ret;
        }
        $ret = curl_exec($ch);
        <span>if</span> (@$paras[<span>'loadurl'</span>]) {
            $Headers = curl_getinfo($ch);
            $ret = $Headers[<span>'redirect_url'</span>];
        }
        curl_close($ch);
        <span>return</span> $ret;
    }</span>

GET访问

<span>echo</span> <span>teacher_curl</span>(<span>"https://api.oioweb.cn/api/beian.php?url=qq.com"</span>);

POST访问

<span>echo</span> teacher_curl(<span>"https://api.oioweb.cn/api/beian.php"</span>,[
        <span>'post'</span>=>[
            <span>'url'</span>=><span>'qq.com'</span>
        ]
    ]);

<span>echo</span> teacher_curl(<span>"https://api.oioweb.cn/api/beian.php"</span>,[
        <span>'post'</span>=><span>'url=qq.com'</span>
    ]);

携带Cookie访问

<span>echo</span> teacher_curl(<span>"https://api.oioweb.cn/api/beian.php?url=qq.com"</span>,[
        <span>'cookie'</span>=><span>'cookie内容'</span>
    ]);

模拟访问来源Refer

<span>echo</span> teacher_curl(<span>"https://api.oioweb.cn/api/beian.php?url=qq.com"</span>,[
        <span>'refer'</span>=><span>'https://api.oioweb.cn'</span>
    ]);

模拟UseaAgent

<span>echo</span> teacher_curl(<span>"https://api.oioweb.cn/api/beian.php?url=qq.com"</span>,[
        <span>'ua'</span>=><span>'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'</span>
    ]);

文件上传

<span>echo</span> teacher_curl(<span>"https://api.oioweb.cn/api/beian.php?url=qq.com"</span>,[
        <span>'post'</span>=>[
            <span>'file'</span>=><span>new</span> CURLFile(realpath(<span>"Curl.jpg"</span>))
        ]
    ]);

<span>echo</span> teacher_curl(<span>"https://api.oioweb.cn/api/beian.php?url=qq.com"</span>,[
        <span>'post'</span>=><span>new</span> CURLFile(realpath(<span>"Curl.jpg"</span>))
    ]);

获取301跳转地址

<span>echo</span> teacher_curl(<span>"https://mmbizurl.cn/s/RNHSo6Dek"</span>,[
        <span>'loadurl'</span>=><span>1</span>
    ]);

查看返回Header信息

<span>echo</span> teacher_curl(<span>"https://api.oioweb.cn/api/beian.php?url=qq.com"</span>,[
        <span>'header'</span>=><span>1</span>
    ]);

设置请求头信息

<span>echo</span> teacher_curl(<span>"https://api.oioweb.cn/api/beian.php?url=qq.com"</span>,[
        <span>'Header'</span>=>[
            <span>'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
    accept-encoding: gzip, deflate, br
    accept-language: zh-CN,zh;q=0.9
    cache-control: max-age=0'</span>
        ]
    ]);

获取请求的全部信息

<span>echo</span> teacher_curl(<span>"https://api.oioweb.cn/api/beian.php?url=qq.com"</span>,[
        <span>'post'</span>=>[
            <span>'user'</span>=><span>123456</span>,
            <span>'pwd'</span>=><span>123</span>
        ],
        <span>'GetCookie'</span>=><span>1</span>
    ]);

最后

还有其他的功能,自己研究吧!还是很强大的!

0

海报

正在生成.....

评论 (0)

语录
取消