Guzzle, PHP HTTP 客户端 — Guzzle中文文档 use GuzzleHttp\Client;
1、发送post方法:
public function actionGetData(){ $client = new Client(); $requestUrl = 'https://www.littleshop.shop/api/v1/users/bonus/rank'; $pageArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]; $header = [ 'authorization'=>'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvd3d3LmxpdHRsZXNob3Auc2hvcFwvYXBpXC92MVwvYXV0aFwvc21zIiwiaWF0IjoxNjQyMTY4MDc1LCJleHAiOjE2NDQ3NjAwNzUsIm5iZiI6MTY0MjE2ODA3NSwianRpIjoiaVJld1RabFVVaVFSenhVdSIsInN1YiI6MjAxNzkwLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.urtPwkfFJ-I6TIuFWkdLWTKpxJw_okhdGM30guppVrc' ]; try { foreach ($pageArr as $page){ $requestData = [ 'page'=>$page, 'scope'=>'', ]; $promises = $client->get($requestUrl,['query' => $requestData,'headers'=>$header]); dd($promises); } }catch (\Exception $e){ dd($e->getMessage()); } }
2、发送post方法:
public function run($url, $query = [], $try = false) { $query = Utils::unsetarray($query); try { $defaults = [ 'headers' => [ 'apikey' => $this->apikey ] ]; $client = new Client(); $options = ArrayHelper::merge($defaults, ['form_params' => $query]); $response = $client->request('POST', $url, $options)->getBody(); $data = json_decode($response->getContents(), true); // \Yii::$app->byfLog->channel('grouplink')->info('请求返回数据'.json_encode($data,JSON_UNESCAPED_UNICODE)); } catch (RequestException $e) { $code = $e->getCode(); // if ($code == 500 && $try === false) { // //如果网络请求500则自动重试一次 // $this->run($url, $query, true); // } throw new \Exception(TbkException::ERR_MSG[TbkException::CORE_PARAMS_INVALID] . $code); } catch (\Exception $e) { TbkException::error(TbkException::CORE_PARAMS_INVALID, $e->getMessage()); } return $data; }
3、多线程发送get请求
$_count = 0;
$tbLInkArr = ['https://*****','https://']; //请求URL地址数组
将数组循环,发送get请求
foreach ($tbLInkArr as $_key=>$goodsId){ $_count += 1; //如果请求接口做了限制,限制次数,调用频率每秒并发不要超过20次 if($_count > 20){ sleep(1); $_count = 0; } //dump($_count); //商品请求 if(!empty($goodsArr['goodsId'][$_key])){ $goodsRequestData['para'] = $goodsArr['goodsId'][$_key]; $goodsRequestData['relation_id'] = $rid; $goodsRequestData['uid'] = $_uid; $promises[] = $client->getAsync($goodsRequestUrl,['query' => $goodsRequestData]); } } //获取请求结果 $results = \GuzzleHttp\Promise\Utils::unwrap($promises); //循环处理请求结果 foreach($results as $key => $result){ $response = $result->getBody()->getContents(); $_response = $response; //错误日志记录返回json值 //获取请求结果 if(empty($response)){ continue; } $response= json_decode($response,true);
}
4、多线程post请求,和get请求一样,只是换了请求名称
//循环数组 $promises = []; foreach ($wphLInkArr as $oldLink){ $data['urlList'] = [$oldLink]; $data['chanTag'] = $_uid; $data['requestId'] = $_uid; $data = array_filter($data); $busiParams = json_encode($data, true); $sysParams["request"] = $busiParams; $sysParams["sign"] = $WphClient->createRequestSign($method, $service, $timestamp, $busiParams); $promises[] = $client->postAsync($requestUrl.'&'.$WphClient->getQueryString($sysParams),['body'=>$busiParams]); } //请求结果 $results = \GuzzleHttp\Promise\Utils::unwrap($promises); $newContent = $content; foreach($results as $key => $result){ $response = $result->getBody()->getContents(); $_response = $response; //获取请求结果 if(empty($response)){ continue; } $response = json_decode($response,true);
//根据业务逻辑处理返回数据
。。。。。
}