laravel guzzle6 使用
1、使用要求
①、php >= 5.5
②、php.ini 中启用 allow_url_fopen
③、curl >= 7.19.4
2、安装
copycomposer require guzzlehttp/guzzle:~6.0
3、发送请求
3.1、快速开始
copyuse GuzzleHttp\Client;
$client = new Client();
$response = $client->request('GET', 'http://xx.com');
$code = $response->getStatusCode();//获取http响应状态码
$data = $response->getBody()->getContents();//获取响应内容
//--GET请求添加参数
$params = [
'headers' => [//请求头信息
'token' => 'VBkmpUn8BPMPPNXJnP8CvneYBpQFDdfnetnxQT3C...'
],
'query' => [//附加参数
'type' => 1
]
];
$response = $client->request('GET','http://xx.com',$params);
//--POST请求添加参数
$params = [
'headers' => [//请求头信息
'token' => 'VBkmpUn8BPMPPNXJnP8CvneYBpQFDdfnetnxQT3C...'
],
'form_params' => [//附加参数
'first_name' => '周末啦',
'last_name' => 'lalala',
'email' => '123@qq.com',
'desc' => '周末啦周末啦周末啦周末啦',
],
'json' => [//传数组
['name'=>'zs','age'=>10],
['name'=>'zs','age'=>10],
['name'=>'zs','age'=>10],
]
];
$response = $client->request('POST','http://loc.nva.com/app/message',$params);
3.2、魔术方法发送
copy$response = $client->get('http://httpbin.org/get');
$response = $client->delete('http://httpbin.org/delete');
$response = $client->head('http://httpbin.org/get');
$response = $client->options('http://httpbin.org/get');
$response = $client->patch('http://httpbin.org/patch');
$response = $client->post('http://httpbin.org/post');
$response = $client->put('http://httpbin.org/put');
3.3、创建请求发送
copy//创建请求发送
use GuzzleHttp\Psr7\Request;
$request = new Request('PUT', 'http://httpbin.org/put');
$response = $client->send($request, ['timeout' => 2]);
4、异步发送请求
4.1、快速开始
copy$client = new Client();
$promise = $client->requestAsync('GET', 'https://xx.com');
$promise->then(
function (ResponseInterface $res) {
echo $res->getStatusCode() . "\n";
\App\Utils\CommonTrait::Logs('StatusCode',$res->getStatusCode(),'pine');
},
function (RequestException $e) {
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
\App\Utils\CommonTrait::Logs('Message',$e->getMessage(),'pine');
}
);
$promise->wait();
4.2、魔术方法发送
copy$promise = $client->getAsync('http://httpbin.org/get');
$promise = $client->deleteAsync('http://httpbin.org/delete');
$promise = $client->headAsync('http://httpbin.org/get');
$promise = $client->optionsAsync('http://httpbin.org/get');
$promise = $client->patchAsync('http://httpbin.org/patch');
$promise = $client->postAsync('http://httpbin.org/post');
$promise = $client->putAsync('http://httpbin.org/put');
4.3、创建请求发送
copyuse GuzzleHttp\Psr7\Request;
$headers = ['X-Foo' => 'Bar'];
$body = 'Hello!';
$request = new Request('HEAD', 'http://httpbin.org/head', $headers, $body);
$promise = $client->sendAsync($request);
$promise = $client->requestAsync('GET', 'http://httpbin.org/get');
5、同时发送多个请求
即并发请求,通过异步实现,避免每次请求等待再进行下次请求
copy$url = 'http://xx.com?id=';
$arr = ['1','2','3','4','5','6'];
$promises = [];
foreach ($arr as $k=>$v){
$tmpUrl = $url.$v;
$promises[$v] = $client->requestAsync('GET', $tmpUrl,$params);
//同时发请求:
//http://xx.com?id=1
//... ...
//http://xx.com?id=6
}
$results = Promise\settle($promises)->wait();//响应结果
foreach ($results as $k=>$v){
if($v['state'] == 'fulfilled'){
$code = $v['value']->getStatusCode();//响应码
$data = $v['value']->getBody()->getContents();//响应内容
dump(['code'=>$code,'data'=>$data]);
}
}
暂时只会那么多,有机会后面补上,详细见官方文档。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构