2025年Guzzle 使用文档

Guzzle 使用文档一 简介 Guzzle 是一个知名度很高的 HTTP 客户端库 有以下特性 支持异步请求 支持并发请求 遵循 PSR 7 标准 不依赖 curl 扩展 二 安装 安装指定版本 composer require guzzlehttp guzzle 7 7 0 安装最新版本 composer require

大家好,我是讯享网,很高兴认识大家。

一、简介

Guzzle 是一个知名度很高的 HTTP 客户端库,有以下特性:

  1. 支持异步请求
  2. 支持并发请求
  3. 遵循PSR-7标准
  4. 不依赖 curl 扩展

二、安装 

三、使用方式 

  1. 发起 Get 请求
use GuzzleHttp\Client as guzzleClient; $guzzleClient = new guzzleClient([ 'timeout' => 2.0 ]); // 同步请求方式 $response = $guzzleClient->get( 'http://api.org/get', [ 'headers' => ['User-Agent' => 'Guzzle'], 'http_errors' => false ] ); $code = $response->getStatusCode(); $body = $response->getBody(); $content = $body->getContents(); // 异步请求方式 $promise = $guzzleClient->getAsync('http://api.org/get'); $promise->then( function (ResponseInterface $res) { echo $res->getStatusCode() . "\n"; }, function (RequestException $e) { echo $e->getMessage() . "\n"; echo $e->getRequest()->getMethod(); } );

讯享网

2. 发起 Post 请求

讯享网use GuzzleHttp\Client as guzzleClient; $guzzleClient = new guzzleClient([ 'timeout' => 2.0 ]); // 原始类型 $response = $guzzleClient->post('http://api.org/post', [ 'body' => 'raw data' ]); // json 类型 application/json $response = $guzzleClient->post('http://api.org/post', [ 'json' => ['name' => 'admin'] ]); // 表单类型 application/x-www-form-urlencoded $response = $guzzleClient->post('http://api.org/post', [ 'form_params' => [ 'username' => 'abc', 'password' => '123' ] ]); // 上传文件 multipart/form-data $response = $guzzleClient->post('http://api.org/post', [ 'multipart' => [ [ 'name' => 'user', 'contents' => 'admin' ], [ 'name' => 'file', 'contents' => fopen('/path/to/file', 'r') ], ] ]); $code = $response->getStatusCode(); $body = $response->getBody(); $content = $body->getContents(); // $content = $response->getBody()->getContents(); 

 3. 并发请求


讯享网

use GuzzleHttp\Pool; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; require 'vendor/autoload.php'; $client = new Client(); $requests = function () { $urls = [ 'https://ip.taobao.com/ipSearch?ipAddr=1.2.3.4', 'https://www.baidu.com', 'https://www.taobao.com', ]; foreach ($urls as $u) { yield new Request('GET', $u); } }; $pool = new Pool($client, $requests(), [ 'concurrency' => 3, 'fulfilled' => function ($response, $index) { // 请求成功 $content = $response->getBody()->getContents(); switch ($index) { case 0: echo "淘宝IP: ", $content, "\n"; break; case 1: echo "百度首页: ", $content, "\n"; break; case 2: echo "淘宝首页: ", $content, "\n"; break; default: # code... break; } }, 'rejected' => function ($reason, $index) { // 请求失败 }, ]); $promise = $pool->promise(); $promise->wait(); 

 4. 使用 Cookie

讯享网$client = new \GuzzleHttp\Client(['cookies' => true]); $response = $client->request('GET', 'http://httpbin.org/cookies');

5. 使用代理

// Win 
set HTTP_PROXY=http://127.0.0.1:1080
set HTTPS_PROXY=http://127.0.0.1:1080

// Linux 
export HTTP_PROXY=http://127.0.0.1:1080
export HTTPS_PROXY=http://127.0.0.1:1080

use GuzzleHttp\Client as guzzleClient; $guzzleClient = new guzzleClient(); $response = $guzzleClient->get('http://api.org/get'); $content = $response->getBody()->getContents(); // $client->request('GET', '/', ['proxy' => 'tcp://localhost:8125']); 

6. 错误处理 。GuzzleHttp请求第三方https接口报错

 解决方法

  1. $client = new \GuzzleHttp\Client(['verify' => false]);
  2. $client->setDefaultOption('verify', false);
小讯
上一篇 2025-02-09 22:01
下一篇 2025-02-18 20:08

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/70493.html