Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Guzzle

1,236 views

Published on

Guzzle 入門です。

Published in: Software
  • Be the first to comment

  • Be the first to like this

Introduction to Guzzle

  1. 1. @DQNEO (どきゅねお) at Mercari 2016.2.22 PHP BLT Introduction to Guzzle
  2. 2. おとといアメリカ出張から 帰国しました (5回目のアメリカ出張) 意識高まったので資料は英語です
  3. 3. Guzzle • http client • sync and async with slimilar interface • Customizable • Huge code base
  4. 4. Huge Code Base
  5. 5. Documentation is a littile bit tough http://docs.guzzlephp.org/
  6. 6. So read the codes! (treasure of design patterns)
  7. 7. Version Latest is ver 6 Use latest !
  8. 8. Depends • guzzlehttp/psr7 • guzzlehttp/promises
  9. 9. Basic Usage $client = new Client(); $response = $client->request('GET', 'http://localhost/', [ 'query' => [ 'name' => 'DQNEO', ] ]); echo $response->getBody(); GET
  10. 10. Basic Usage $client = new Client(); $response = $client->request('POST', 'http://localhost/', [ 'form_params' => [ 'name' => 'DQNEO', ] ]); echo $response->getBody(); POST (x-www-form-urlencoded)
  11. 11. Basic Usage $client = new Client(); $response = $client->request('POST', 'http://localhost/', [ 'json' => [ 'name' => 'DQNEO', ] ]); echo $response->getBody(); POST (json body)
  12. 12. Basic Usage $client = new Client(); $response = $client->request('PUT', 'http://localhost/', [ 'json' => [ 'name' => 'DQNEO', ] ]); echo $response->getBody(); PUT (json body)
  13. 13. Asynchronous Guzzle Promiseを使った 非同期処理によるAPIコールの高速化 com/suzuki/guzzle-promisewoshi-tuta-fei-tong-qi-chu-li-niyoruapik
  14. 14. How to test
  15. 15. Mock Handler $handler = new MockHandler([ new Response(200, ['X-Foo' => 'Bar']), new Response(202, ['Content-Length' => 0]), new RequestException("Error", new Request('GET', 'test')) ]);
  16. 16. use GuzzleHttpClient; use GuzzleHttpHandlerMockHandler; use GuzzleHttpPsr7Response; use GuzzleHttpPsr7Request; use GuzzleHttpExceptionRequestException; $handler = new MockHandler([ new Response(200, ['X-Foo' => 'Bar']), new Response(202, ['Content-Length' => 0]), new RequestException("Error", new Request('GET', 'test')) ]); $client = new Client(['handler' => $handler]); $response = $client->request('GET', '/'); echo $response->getStatusCode() . PHP_EOL; // => 200 $response = $client->request('GET', '/'); echo $response->getStatusCode() . PHP_EOL; // => 202 $response = $client->request('GET', '/'); // => RequestException Mock Handler
  17. 17. Handler is the essence of guzzle $client = new Client(['handler' => $handler]);
  18. 18. Handler you can pass a callable $client = new Client([ 'handler' => $callable ]);
  19. 19. Handler spec of handler handler: (callable) Function that transfers HTTP requests over the wire. The function is called with a Psr7HttpMessageRequestInterface and array of transfer options, and must return a GuzzleHttpPromisePromiseInterface
  20. 20. Handler spec of handler function (RequestInterface $request, array $options) { return $promise_interface; } or public function __invoke( RequestInterface $request, array $options ) { return $promise_interface; }
  21. 21. Handler • Closure • CurlHandler • CurlMultiHandler • StreamHandler • MockHandler examples
  22. 22. Handler should be callable $client = new Client([ 'handler' => 1 ]); PHP Fatal error: Uncaught InvalidArgumentException: handler must be a callable
  23. 23. Handler I implmented the type check
  24. 24. Huge Code Base
  25. 25. So lots of chances to contribute
  26. 26. Read Code and Make Contributions!
  27. 27. We are hiring!

×