批量curl请求

来源:互联网 发布:淘宝改一口价的技巧 编辑:程序博客网 时间:2024/05/21 01:29
//更新价格public function batchupdateSkuPrice($arr){    $url = $this->uri;    $method = 'updateSkuPrice';    $start = microtime(true);    $ch_list = array();    $multi_ch = curl_multi_init();    for ($i = 0;$i < count($arr);++$i) {        $ch_list[$i] = curl_init($url);        curl_setopt($ch_list[$i], CURLOPT_URL, $url);        curl_setopt($ch_list[$i], CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch_list[$i], CURLOPT_FOLLOWLOCATION, true);        curl_setopt($ch_list[$i], CURLOPT_HTTPHEADER, $header = array());        curl_setopt($ch_list[$i], CURLOPT_POST, 1);        curl_setopt($ch_list[$i], CURLOPT_POSTFIELDS,            $data = array(            'token'         =>      Session::get('lt_token.token'),            'method'        =>      $method,            'skuPriceInfo'  =>      json_encode($arr[$i])        ));        curl_setopt($ch_list[$i], CURLOPT_SSL_VERIFYPEER, false);        curl_setopt($ch_list[$i],CURLOPT_SSL_VERIFYHOST,false);        curl_multi_add_handle($multi_ch, $ch_list[$i]);        if(file_exists("/tmp"))            file_put_contents("/tmp/lt_savePrice.log", date("Y-m-d H:i:s").var_export($data,true)."<br>\n", FILE_APPEND);    }    $active = null;    do {        $mrc = curl_multi_exec($multi_ch, $active); //处理在栈中的每一个句柄。无论该句柄需要读取或写入数据都可调用此方法。    } while ($mrc == CURLM_CALL_MULTI_PERFORM);    //Note:    //该函数仅返回关于整个批处理栈相关的错误。即使返回 CURLM_OK 时单个传输仍可能有问题。    while ($active && $mrc == CURLM_OK) {        if (curl_multi_select($multi_ch) != -1) { //阻塞直到cURL批处理连接中有活动连接。            do {                $mrc = curl_multi_exec($multi_ch, $active);            } while ($mrc == CURLM_CALL_MULTI_PERFORM);        }    }    $res = array();    //获取http返回的结果    foreach ($ch_list as $k => $ch) {        $result=curl_multi_getcontent($ch);        curl_multi_remove_handle($multi_ch,$ch);        curl_close($ch);        $res[] = json_decode($result,true);    }    curl_multi_close($multi_ch);    $end = microtime(true);    //print_r($res);exit();    return $res;}
原创粉丝点击