php 的 curl 并发请求

臭大佬 2021-12-28 21:38:53 517
php 
简介 php 的 curl 并发请求

并发请求


     /**
     * 多线程检测
     * Date: 2021/12/15
     * Time: 23:58
     * @param array $arr 数组
     * @param int $timeout
     * @return mixed
     */
public function curlMulCheck(string $url,array $arr,int $timeout = 5)
    {
        $header = [];
        $header[] = 'Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*';
        $header[] = 'Connection: Keep-Alive';
        $header[] = 'Accept-Language: zh-cn';
        $header[] = 'Cache-Control: no-cache';
        $mh = curl_multi_init(); //创建多个curl语柄
        foreach ($arr as $k => $v) {
            // 假设每个值是传递的id
            $url = $url.'?id='.$v;
            $conn[$k] = curl_init($url);
            if (stripos($url, "https://") !== FALSE) {
                curl_setopt($conn[$k], CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($conn[$k], CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($conn[$k], CURLOPT_SSLVERSION, 1);
            }
            curl_setopt($conn[$k], CURLOPT_HEADER, true); //如果不为 true, 只会获得响应的正文
            curl_setopt($conn[$k], CURLOPT_TIMEOUT, $timeout); //设置超时时间
            curl_setopt($conn[$k], CURLOPT_HTTPHEADER, $header);
            curl_setopt($conn[$k], CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($conn[$k], CURLOPT_RETURNTRANSFER, true);
            curl_setopt($conn[$k], CURLOPT_REFERER, $url);
            curl_setopt($conn[$k], CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2');
            curl_multi_add_handle($mh, $conn[$k]);
        }
        // 执行批处理句柄
        $active = null;
        do {
            $mrc = curl_multi_exec($mh, $active); //当无数据,active=true
        } while ($mrc == CURLM_CALL_MULTI_PERFORM); //当正在接受数据时
        while ($active && $mrc == CURLM_OK) { //当无数据时或请求暂停时,active=true
            if (curl_multi_select($mh) != -1) {
                do {
                    $mrc = curl_multi_exec($mh, $active);
                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
            }
        }
        foreach ($arr as $k => $url) {
            curl_error($conn[$k]);
            //echo curl_error($conn[$k]);die;
            //$gg = curl_getinfo($conn[$k], CURLINFO_REDIRECT_URL);
            //var_dump($conn[$k]);die;
            $content = curl_multi_getcontent($conn[$k]); //获得返回信息
            $headerSize = curl_getinfo($conn[$k], CURLINFO_HEADER_SIZE); // 获得响应结果里的:头大小
            $header = substr($content, 0, $headerSize);
           // 逻辑代码
           // ...
            curl_multi_remove_handle($mh, $conn[$k]); //释放资源
            curl_close($conn[$k]); //关闭语柄
        }

        curl_multi_close($mh);
        return $resDoamin;
    }

get、post请求

    /**
     * curl请求
     * Date: 2021/12/28
     * Time: 17:26
     * @param string $url 请求地址
     * @param string $method 请求方式 GET|POST
     * @param array $param 请求参数
     * @param string $origin origin
     * @param string $referer referer
     * @param null $cookie
     * @return bool|string
     */
    public function curlExecute(string $url, string $method = 'GET',array $param = [], string $origin = '',string  $referer = '', $cookie = null)
    {
        $curl = curl_init();
        $header = [
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36',
            'Accept: application/json, text/javascript, */*; q=0.01',
            'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            'Accept-Encoding: gzip, deflate, br',
            'X-Requested-With: XMLHttpRequest',
            'Connection: keep-alive',
            'Sec-Fetch-Dest: empty',
            'Sec-Fetch-Mode: cors',
            'Sec-Fetch-Site: same-origin ',
            'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
        ];
        if ($origin) {
            $header[] = 'Origin: ' . $origin;
        }
        if ($referer) {
            $header[] = 'Referer: ' . $referer;
        }
        if ($cookie) {
            $header[] = 'Cookie: ' . $cookie;
        }
        $curloptArr = [
            CURLOPT_SSL_VERIFYPEER => 0,
            CURLOPT_HEADER => 1,
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $method,
            CURLOPT_HTTPHEADER => $header,
        ];
        if ($method == 'POST') {
            $curloptArr[CURLOPT_POSTFIELDS] = http_build_query($param);
        }
        curl_setopt_array($curl, $curloptArr);
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

使用方法

$response = $this->curlExecute($url, 'POST', $param, $url, $url, $cookie);
// 分离header和body
list($header, $body) = explode("\r\n\r\n", $response, 2);
// 提取cookie
$preg_cookie = '/Set-Cookie: (.*?);/m';
preg_match_all($preg_cookie, $header, $cookie);
// 得到cookie 字符串
$cookieStr = implode(';', $cookie[1]);