您好,登錄后才能下訂單哦!
這篇文章主要介紹了Guzzle如何安裝和使用的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Guzzle如何安裝和使用文章都會有所收獲,下面我們一起來看看吧。
Guzzle是一個PHP HTTP客戶端,可以輕松發送HTTP請求,并且可以輕松集成Web服務。
1.使用composer安裝
composer require guzzlehttp/guzzle
2.或者編輯項目的composer.json文件,添加Guzzle作為依賴
{ "require": { "guzzlehttp/guzzle": "~6.0" } }
執行 composer update
1.發送請求
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');
2.設置查詢字符串
$response = $client->request('GET', 'http://httpbin.org?foo=bar');
或使用 query 請求參數來聲明查詢字符串參數:
$client->request('GET', 'http://httpbin.org', [ 'query' => ['foo' => 'bar'] ]);
3.設置POST表單,傳入 form_params 數組參數
$response = $client->request('POST', 'http://httpbin.org/post', [ 'form_params' => [ 'field_name' => 'abc', 'other_field' => '123', 'nested_field' => [ 'nested' => 'hello' ] ] ]);
4.使用響應
# 狀態碼 $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();
composer方式安裝
composer global require "phpunit/phpunit=5.5.*"
或者在composer.json文件中聲明對phpunit/phpunit的依賴
{ "require-dev": { "phpunit/phpunit": "5.5.*" } }
1.我們在tests\unit\MyApiTest.php中定義了兩個測試用例
<?php class MyApiTest extends \PHPUnit_Framework_TestCase { protected $client; public function setUp() { $this->client = new \GuzzleHttp\Client( [ 'base_uri' => 'http://myhost.com', 'http_errors' => false, #設置成 false 來禁用HTTP協議拋出的異常(如 4xx 和 5xx 響應),默認情況下HTPP協議出錯時會拋出異常。 ]); } public function testAction1() { $response = $this->client->get('/api/v1/action1'); $body = $response->getBody(); //添加測試 $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); $this->assertArrayHasKey('errorno', $data); $this->assertArrayHasKey('errormsg', $data); $this->assertArrayHasKey('data', $data); $this->assertEquals(0, $data['errorno']); $this->assertInternalType('array', $data['data']); } public function testAction2() { $response = $this->client->post('/api/v1/action2', [ 'form_params' => [ 'name' => 'myname', 'age' => 20, ], ]); $body = $response->getBody(); //添加測試 $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); $this->assertArrayHasKey('errorno', $data); $this->assertArrayHasKey('errormsg', $data); $this->assertArrayHasKey('data', $data); $this->assertEquals(0, $data['errorno']); $this->assertInternalType('array', $data['data']); } }
2.運行測試
在項目根目錄執行命令
php vendor/bin/phpunit tests/unit/MyApiTest.php
關于“Guzzle如何安裝和使用”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Guzzle如何安裝和使用”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。