php curl 多次发送


public function curls()
    {
        $urls = "http://wyh.li/review/save_all_agree?act=do";
       
        foreach (range(1, 100) as $vals) {
            $expert_id = $vals;
            $mulcurl[] = $this->singleCurl($urls, $expert_id);
        }

        $data = $this->multiCurl($mulcurl);

        var_dump($data);
    }


    public function singleCurl($url, $expert_id = 0)
    {
        $data['is_all_agree'] = 1;
        $data['list_type'] = 3;
        $data['expert_id'] = $expert_id;


        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        // $output = curl_exec($curl);

        // var_dump($output);
        return $curl;
    }

    //多进程curl
    public function multiCurl($ch_arr)
    {
        $mh = curl_multi_init();
        foreach ($ch_arr as $k => $c) {
            curl_multi_add_handle($mh, $ch_arr[$k]);
        }
        $active = null;
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);

        while ($active && $mrc == CURLM_OK) {
            usleep(50000);
            if (curl_multi_select($mh) != -1) {
                do {
                    $mrc = curl_multi_exec($mh, $active);
                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
            }
        }

        foreach ($ch_arr as $k => $v) {
            if (curl_error($ch_arr[$k]) == '') {
                $res[$k] = json_decode(curl_multi_getcontent($ch_arr[$k]), true);
            }
            curl_close($ch_arr[$k]);
        }
        curl_multi_close($mh);
        return $res;
    }
posted @ 2023-07-24 14:58  尘梦  阅读(40)  评论(0编辑  收藏  举报