O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
@DQNEO (どきゅねお) at Mercari
2016.2.22 PHP BLT
Introduction to Guzzle
おとといアメリカ出張から
帰国しました
(5回目のアメリカ出張)
意識高まったので資料は英語です
Guzzle
• http client
• sync and async with slimilar interface
• Customizable
• Huge code base
Huge Code Base
Documentation is
a littile bit tough
http://docs.guzzlephp.org/
So read the codes!
(treasure of design patterns)
Version
Latest is ver 6
Use latest !
Depends
• guzzlehttp/psr7
• guzzlehttp/promises
Basic Usage
$client = new Client();
$response = $client->request('GET', 'http://localhost/', [
'query' => [
'name' => 'DQN...
Basic Usage
$client = new Client();
$response = $client->request('POST', 'http://localhost/', [
'form_params' => [
'name' ...
Basic Usage
$client = new Client();
$response = $client->request('POST', 'http://localhost/', [
'json' => [
'name' => 'DQN...
Basic Usage
$client = new Client();
$response = $client->request('PUT', 'http://localhost/', [
'json' => [
'name' => 'DQNE...
Asynchronous
Guzzle Promiseを使った
非同期処理によるAPIコールの高速化
com/suzuki/guzzle-promisewoshi-tuta-fei-tong-qi-chu-li-niyoruapik
How to test
Mock Handler
$handler = new MockHandler([
new Response(200, ['X-Foo' => 'Bar']),
new Response(202, ['Content-Length' => 0]...
use GuzzleHttpClient;
use GuzzleHttpHandlerMockHandler;
use GuzzleHttpPsr7Response;
use GuzzleHttpPsr7Request;
use GuzzleH...
Handler
is the essence of guzzle
$client = new Client(['handler' => $handler]);
Handler
you can pass a callable
$client = new Client([
'handler' => $callable
]);
Handler
spec of handler
handler:
(callable) Function that transfers HTTP requests over the wire.
The function is called wi...
Handler
spec of handler
function (RequestInterface $request, array $options)
{
return $promise_interface;
}
or
public func...
Handler
• Closure
• CurlHandler
• CurlMultiHandler
• StreamHandler
• MockHandler
examples
Handler
should be callable
$client = new Client([
'handler' => 1
]);
PHP Fatal error: Uncaught InvalidArgumentException: h...
Handler
I implmented the type check
Huge Code Base
So lots of chances to
contribute
Read Code
and
Make Contributions!
We are hiring!
Próximos SlideShares
Carregando em…5
×

Introduction to Guzzle

1.383 visualizações

Publicada em

Guzzle 入門です。

Publicada em: Software
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

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!

×