Guzzle

Guzzle HTTP 客户端全解析及 PHP 使用与优化 一、介绍 Guzzle 是 PHP 世界中最流行的 HTTP 客户端库之一,用于发送 HTTP/HTTPS 请求并处理响应。 它支持: 同步和异步请求 HTTP/2 中间件机制(类似 Laravel / Hyperf 的管道) 并发请求 请求重试、超时、连接池 Guzzle 常被用于调用第三方 API、微服务接口或爬取网页数据。 官方文档:https://docs.guzzlephp.org 二 、安装 composer require guzzlehttp/guzzle 三、基本使用 3.1 同步 GET 请求 use GuzzleHttp\Client; $client = new Client([ 'base_uri' => 'https://api.example.com', 'timeout' => 5.0, // 超时时间 ]); $response = $client->get('/users/1'); echo $response->getStatusCode(); // 200 echo $response->getBody(); // JSON 或 HTML 3.2 POST 请求与 JSON 数据 $response = $client->post('/users', [ 'json' => [ 'name' => 'John', 'email' => 'john@example....

November 15, 2022 · 2 min · Leanku