ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

guzzlehttp/guzzle发送请求说明

2022-04-25 14:33:34  阅读:220  来源: 互联网

标签:httpbin http guzzlehttp 发送 client guzzle org echo response


composer require guzzlehttp/guzzle


Guzzle是一个PHP HTTP客户端,可以轻松发送HTTP请求,并且可以轻松集成Web服务。

用于构建查询字符串,POST请求,流式传输大型上传,流式传输大型下载,使用HTTP cookie,上传JSON数据等的简单界面...
可以使用相同的接口发送同步和异步请求。
为请求,响应和流使用PSR-7接口。这使您可以与Guzzle一起使用其他PSR-7兼容库。
抽象出底层的HTTP传输,允许您编写环境和传输不可知的代码; 即不依赖于cURL,PHP流,套接字或非阻塞事件循环。
中间件系统允许您增强和编写客户端行为。

编辑项目的composer.json文件,添加Guzzle作为依赖

{
   "require": {
      "guzzlehttp/guzzle": "~6.0"
   }
}

Guzzle基本使用

  • 发送请求
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'
 
// 发送一个异步请求
$request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
$promise = $client->sendAsync($request)->then(function ($response) {
    echo 'I completed! ' . $response->getBody();
});
$promise->wait();
use GuzzleHttp\Client;
 
$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'http://httpbin.org',
    // You can set any number of default request options.
    'timeout'  => 2.0,
]);
 
$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');

设置查询字符串

$response = $client->request('GET', 'http://httpbin.org?foo=bar');

或使用 query 请求参数来声明查询字符串参数:

$client->request('GET', 'http://httpbin.org', [
'query' => ['foo' => 'bar']
]);

设置POST表单
传入 form_params 数组参数

$response = $client->request('POST', 'http://httpbin.org/post', [
  'form_params' => [
  'field_name' => 'abc',
  'other_field' => '123',
  'nested_field' => [
  'nested' => 'hello'
]
]
]);
  • 使用响应
# 状态码
$code = $response->getStatusCode(); // 200
$reason = $response->getReasonPhrase(); // OK
 
# header
// Check if a header exists.
if ($response->hasHeader('Content-Length')) {
    echo "It exists";
}
 
// Get a header from the response.
echo $response->getHeader('Content-Length');
 
// Get all of the response headers.
foreach ($response->getHeaders() as $name => $values) {
    echo $name . ': ' . implode(', ', $values) . "\r\n";
}
 
# 响应体
$body = $response->getBody();
// Implicitly cast the body to a string and echo it
echo $body;
// Explicitly cast the body to a string
$stringBody = (string) $body;
// Read 10 bytes from the body
$tenBytes = $body->read(10);
// Read the remaining contents of the body as a string
$remainingBytes = $body->getContents();

 

标签:httpbin,http,guzzlehttp,发送,client,guzzle,org,echo,response
来源: https://www.cnblogs.com/akidongzi/p/16190078.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有