// 同步请求方式
$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();
发起 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')
安装composer require guzzlehttp/guzzle:7.0.1发起 Get 请求use GuzzleHttp\Client as guzzleClient;$guzzleClient = new guzzleClient(['timeout' => 2.0]);// 同步请求方式$response = $guzzleClient->get('http://api....
use
Guzzle
Http
\ Client ;
use
Guzzle
Http
\ Message \ Response ;
use React \ EventLoop \ Factory as EventLoopFactory ;
use WyriHaximus \ S3ParallelUpload \ Uploader ;
use WyriHaximus \ React \ Ring
PHP
\
Http
ClientAdapt
name: (string, required) 表单字段名称
contents: (StreamInterface/resource/string, required) 表单元素中要
使用
的数据
headers: (array) 可选的表
接口简单:构建查询语句、POST请求、分流上传下载大文件、
使用
HTTP
cookies、上传JSON数据等等。
发送同步或异步的请求均
使用
相同的接口。
使用
PSR-7接口来请求、响应、分流,允许你
使用
其他兼容的PSR-7类库与
Guzzle
共同开发。
抽象了底层的
HTTP
传输,允许你改变环境以及其他的代码,如:对cURL与
PHP
的流或socket并非重度依赖,非阻塞事件循环。
中间件系统允
可以
使用
PHP
调用ChatGPT,例如
使用
Guzzle
库,可以
使用
以下代码:$client = new
Guzzle
Http
\Client();$response = $client->post('
http
s://api.openai.com/v1/engines/chatgpt/completions',[ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key, ], 'json' => [ 'prompt' => $prompt, 'max_tokens' => $max_tokens, 'temperature' => $temperature, 'top_p' => $top_p, 'n' => $n, 'stream' => $stream, 'logprobs' => $logprobs, ], ]);$response_json = $response->getBody()->getContents();$response_data = json_decode($response_json,true);