Guzzle如何安装和使用

后端开发   发布日期:2023年06月23日   浏览次数:502

这篇文章主要介绍了Guzzle如何安装和使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Guzzle如何安装和使用文章都会有所收获,下面我们一起来看看吧。

一.什么是guzzle

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

二.安装Guzzle

1.使用composer安装

  1. composer require guzzlehttp/guzzle

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

  1. {
  2. "require": {
  3. "guzzlehttp/guzzle": "~6.0"
  4. }
  5. }

执行 composer update

三.Guzzle基本使用

1.发送请求

  1. use GuzzleHttpClient;
  2. $client = new Client([
  3. // Base URI is used with relative requests
  4. 'base_uri' => 'http://httpbin.org',
  5. // You can set any number of default request options.
  6. 'timeout' => 2.0,
  7. ]);
  8. $response = $client->get('http://httpbin.org/get');
  9. $response = $client->delete('http://httpbin.org/delete');
  10. $response = $client->head('http://httpbin.org/get');
  11. $response = $client->options('http://httpbin.org/get');
  12. $response = $client->patch('http://httpbin.org/patch');
  13. $response = $client->post('http://httpbin.org/post');
  14. $response = $client->put('http://httpbin.org/put');

2.设置查询字符串

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

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

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

3.设置POST表单,传入 form_params 数组参数

  1. $response = $client->request('POST', 'http://httpbin.org/post', [
  2. 'form_params' => [
  3. 'field_name' => 'abc',
  4. 'other_field' => '123',
  5. 'nested_field' => [
  6. 'nested' => 'hello'
  7. ]
  8. ]
  9. ]);

4.使用响应

  1. # 状态码
  2. $code = $response->getStatusCode(); // 200
  3. $reason = $response->getReasonPhrase(); // OK
  4. # header
  5. // Check if a header exists.
  6. if ($response->hasHeader('Content-Length')) {
  7. echo "It exists";
  8. }
  9. // Get a header from the response.
  10. echo $response->getHeader('Content-Length');
  11. // Get all of the response headers.
  12. foreach ($response->getHeaders() as $name => $values) {
  13. echo $name . ': ' . implode(', ', $values) . "
  14. ";
  15. }
  16. # 响应体
  17. $body = $response->getBody();
  18. // Implicitly cast the body to a string and echo it
  19. echo $body;
  20. // Explicitly cast the body to a string
  21. $stringBody = (string) $body;
  22. // Read 10 bytes from the body
  23. $tenBytes = $body->read(10);
  24. // Read the remaining contents of the body as a string
  25. $remainingBytes = $body->getContents();

四.安装PHPUnit

composer方式安装

  1. composer global require "phpunit/phpunit=5.5.*"

或者在composer.json文件中声明对phpunit/phpunit的依赖

  1. {
  2. "require-dev": {
  3. "phpunit/phpunit": "5.5.*"
  4. }
  5. }

五.API单元测试

1.我们在testsunitMyApiTest.php中定义了两个测试用例

  1. <?php
  2. class MyApiTest extends PHPUnit_Framework_TestCase
  3. {
  4. protected $client;
  5. public function setUp()
  6. {
  7. $this->client = new GuzzleHttpClient( [
  8. 'base_uri' => 'http://myhost.com',
  9. 'http_errors' => false, #设置成 false 来禁用HTTP协议抛出的异常(如 4xx 和 5xx 响应),默认情况下HTPP协议出错时会抛出异常。
  10. ]);
  11. }
  12. public function testAction1()
  13. {
  14. $response = $this->client->get('/api/v1/action1');
  15. $body = $response->getBody();
  16. //添加测试
  17. $this->assertEquals(200, $response->getStatusCode());
  18. $data = json_decode($body, true);
  19. $this->assertArrayHasKey('errorno', $data);
  20. $this->assertArrayHasKey('errormsg', $data);
  21. $this->assertArrayHasKey('data', $data);
  22. $this->assertEquals(0, $data['errorno']);
  23. $this->assertInternalType('array', $data['data']);
  24. }
  25. public function testAction2()
  26. {
  27. $response = $this->client->post('/api/v1/action2', [
  28. 'form_params' => [
  29. 'name' => 'myname',
  30. 'age' => 20,
  31. ],
  32. ]);
  33. $body = $response->getBody();
  34. //添加测试
  35. $this->assertEquals(200, $response->getStatusCode());
  36. $data = json_decode($body, true);
  37. $this->assertArrayHasKey('errorno', $data);
  38. $this->assertArrayHasKey('errormsg', $data);
  39. $this->assertArrayHasKey('data', $data);
  40. $this->assertEquals(0, $data['errorno']);
  41. $this->assertInternalType('array', $data['data']);
  42. }
  43. }

2.运行测试
在项目根目录执行命令

  1. php vendor/bin/phpunit tests/unit/MyApiTest.php

以上就是Guzzle如何安装和使用的详细内容,更多关于Guzzle如何安装和使用的资料请关注九品源码其它相关文章!