curl_multi_init 笔记

日期:2019-03-29 浏览:385次
//请求函数
public function exec_int()
{
    //$result = $this->rand_otc(1);
    $result = ['weixin_openid'=>'wx_openid45465465f4sdf'];
    return json_encode($result);
}
//执行方法
public function test_curl()
{
    set_time_limit(0);
    Debug::remark('begin');
    // 设置URL和相应的选项
    $url = 'http://www.test.com/index.php/index/test/exec_int';
    $active = null;
    $mh = curl_multi_init();
    for($j = 0; $j < 5000; $j++) {
        $data=[];
        for ($i = 0; $i < 10; $i++) {
            $ch[$i] = curl_init();
            curl_setopt($ch[$i], CURLOPT_URL, $url);
            curl_setopt($ch[$i], CURLOPT_POST, true); // 是否为post请求
            curl_setopt($ch[$i], CURLOPT_POSTFIELDS, '1'); // 发送post数据
            curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch[$i], CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($ch[$i], CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch[$i], CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch[$i], CURLOPT_MAXREDIRS, 10);
            curl_setopt($ch[$i], CURLOPT_AUTOREFERER, 1);
            curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
            curl_multi_add_handle($mh, $ch[$i]);
        }
        //执行批处理句柄
        $active = null;

        do {
            while (($mrc = curl_multi_exec($mh, $active)) == CURLM_CALL_MULTI_PERFORM) ;

            if ($mrc != CURLM_OK) {
                break;
            }
            // a request was just completed -- find out which one
            while ($done = curl_multi_info_read($mh)) {
                //return '执行了' . date('i');
                // get the info and content returned on the request
                //$info = curl_getinfo($done['handle']);
                //$error = curl_error($done['handle']);
                //$result[] = curl_multi_getcontent($done['handle']);
                $result = json_decode(curl_multi_getcontent($done['handle']),true);
                /*echo $result['weixin_openid'];
                ob_flush();
                flush();*/
                $data[] = $result['weixin_openid'];
                /*if (!empty()){
                    Db::name('test_curl')->insert(['name' => $result['weixin_openid']]);
                }*/
                $result = null;
                //Db::name('test_curl')->insert(['name' => $info]);
                // $responses[$map[(string) $done['handle']]] = compact('info', 'error', 'results');

                // remove the curl handle that just completed
                curl_multi_remove_handle($mh, $done['handle']);
                curl_close($done['handle']);
                usleep(10);
            }
            // Block for data in / output; error handling is done by curl_multi_exec
            if ($active > 0) {
                curl_multi_select($mh);
            }

        } while ($active);
        $insert_data = '(\'';
        foreach ($data as $val){
            $insert_data .= $val.'\'),(\'';
        }
        $insert_data = rtrim($insert_data,',(\'');
        $sql = 'insert into xhd_test_curl (`name`) VALUES '.$insert_data;
        Db::execute($sql);
        $data = null;

    }
    curl_multi_close($mh);
    Debug::remark('end');
    echo Debug::getRangeTime('begin','end').'s','<br >';
    echo Debug::getRangeMem('begin','end');
}

没有评论

发表评论