Guzzle的简单使用

概述

Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送HTTP请求。
中文文档地址:https://guzzle-cn.readthedocs.io/zh_CN/latest/index.html

环境要求

  • PHP >= 5.5.0
  • 使用PHP的流, allow_url_fopen 必须在php.ini中启用。
  • 要使用cURL,你必须已经有版本cURL >= 7.19.4,并且编译了OpenSSL 与 zlib。

安装

安装方法一:使用composer安装(略)
安装方法二:直接下载,require。

1.进入下载页面

进入网址:https://php-download.com/,在搜索框中搜索“guzzle”,点击“GO TO DOWNLOAD”按钮:

2.选择版本

3.下载并解压:

简单示例

发送GET请求

  1. <?php
  2. require_once("vendor/autoload.php");
  3. use GuzzleHttp\Client;
  4. $client = new Client(['verify'=>false]);
  5. $res = $client->request('GET', 'https://api.github.com/user', [
  6. 'auth' => ['user', 'password']
  7. ]);
  8. //获取响应状态码
  9. echo $res->getStatusCode().'<hr>';
  10. //获取响应头部信息
  11. $header = $res->getHeader('content-type');
  12. echo $header[0].'<hr>';
  13. //get all headers: $res->getHeaders();
  14. //获取响应主体
  15. echo $res->getBody();

结果如下:

发送一个异步请求

  1. <?php
  2. require_once("vendor/autoload.php");
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Psr7\Request;
  5. $client = new Client(['verify'=>false]);
  6. $request = new Request('GET', 'http://httpbin.org/get');
  7. $promise = $client->sendAsync($request)->then(function ($response) {
  8. echo 'I completed! ' . $response->getBody();
  9. });
  10. $promise->wait();

结果:

  1. I completed! { "args": {}, "headers": { "Host": "httpbin.org", "User-Agent": "GuzzleHttp/6.3.3 curl/7.50.1 PHP/5.6.25" }, "origin": "118.122.250.228, 118.122.250.228", "url": "https://httpbin.org/get" }

发送一个POST请求

  1. <?php
  2. require_once("vendor/autoload.php");
  3. use GuzzleHttp\Client;
  4. $client = new Client(['verify'=>false]);
  5. $res = $client->request('POST', 'http://httpbin.org/post',[
  6. 'form_params' => [
  7. 'field_name' => 'abc',
  8. 'other_field' => '123',
  9. 'nested_field' => [
  10. 'nested' => 'hello'
  11. ]
  12. ]
  13. ]);
  14. echo $res->getBody();

结果:

  1. { "args": {}, "data": "", "files": {}, "form": { "field_name": "abc", "nested_field[nested]": "hello", "other_field": "123" }, "headers": { "Content-Length": "61", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "GuzzleHttp/6.3.3 curl/7.50.1 PHP/5.6.25" }, "json": null, "origin": "118.122.250.228, 118.122.250.228", "url": "https://httpbin.org/post" }

发送并发请求

有时候我只想发送一个请求,并不想获取到响应结果,例如发送短信,发送邮件等。那么可以将timeout参数设置为小一点,例如1秒。

  1. <?php
  2. require_once("vendor/autoload.php");
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Promise;
  5. $client = new Client([
  6. 'base_uri' => 'http://php70.jay520.com/',
  7. 'timeout' => 1,
  8. 'verify'=>false
  9. ]);
  10. // Initiate each request but do not block
  11. $data = [
  12. 'form_params' => [
  13. 'field_name' => 'abc',
  14. 'other_field' => '123',
  15. 'nested_field' => [
  16. 'nested' => 'hello'
  17. ]
  18. ]
  19. ];
  20. $promises = [
  21. 'post1' => $client->requestAsync('POST','/guzzle/post1.php',$data),
  22. 'post2' => $client->requestAsync('POST','/guzzle/post2.php',$data),
  23. 'post3' => $client->requestAsync('POST','/guzzle/post3.php',$data),
  24. ];
  25. // Wait on all of the requests to complete.
  26. //$results = Promise\unwrap($promises);
  27. $results = Promise\settle($promises)->wait();
  28. echo 'finish';exit();

《Guzzle的简单使用》上有2条评论

  1. 我想知道发送了一个异步请求,但是我怎么确定这个异步的接口请求成功获取了数据,怎么渲染到blade页面上

发表评论

邮箱地址不会被公开。 必填项已用*标注

发表评论前,请滑动滚动条解锁
三十岁