Introduction to Guzzle

27
@DQNEO ( どどどどど ) at Mercari 2016.2.22 PHP BLT Introduction to Guzzle

Transcript of Introduction to Guzzle

@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 isa littile bit tough

http://docs.guzzlephp.org/

So read the codes!(treasure of design patterns)

VersionLatest is ver 6Use latest !

Depends• guzzlehttp/psr7• guzzlehttp/promises

Basic Usage

$client = new Client();$response = $client->request('GET', 'http://localhost/', [ 'query' => [ 'name' => 'DQNEO', ]]);echo $response->getBody();

GET

Basic Usage

$client = new Client();$response = $client->request('POST', 'http://localhost/', [ 'form_params' => [ 'name' => 'DQNEO', ]]);echo $response->getBody();

POST (x-www-form-urlencoded)

Basic Usage

$client = new Client();$response = $client->request('POST', 'http://localhost/', [ 'json' => [ 'name' => 'DQNEO', ]]);echo $response->getBody();

POST (json body)

Basic Usage

$client = new Client();$response = $client->request('PUT', 'http://localhost/', [ 'json' => [ 'name' => 'DQNEO', ]]);echo $response->getBody();

PUT (json body)

AsynchronousGuzzle Promiseを使った非同期処理による APIコールの高速化

https://speakerdeck.com/suzuki/guzzle-promisewoshi-tuta-fei-tong-qi-chu-li-niyoruapikorufalsegao-su-hua

How to test

Mock Handler

$handler = new MockHandler([ new Response(200, ['X-Foo' => 'Bar']), new Response(202, ['Content-Length' => 0]), new RequestException("Error", new Request('GET', 'test'))]);

use GuzzleHttp\Client;use GuzzleHttp\Handler\MockHandler;use GuzzleHttp\Psr7\Response;use GuzzleHttp\Psr7\Request;use GuzzleHttp\Exception\RequestException;

$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

Handler

is the essence of guzzle

$client = new Client(['handler' => $handler]);

Handleryou can pass a callable

$client = new Client([ 'handler' => $callable]);

Handlerspec of handlerhandler: (callable) Function that transfers HTTP requests over the wire.

The function is called with a Psr7\Http\Message\RequestInterface and array of transfer options,

and must return a GuzzleHttp\Promise\PromiseInterface

Handlerspec of handlerfunction (RequestInterface $request, array $options){ return $promise_interface;}

or

public function __invoke( RequestInterface $request, array $options){ return $promise_interface;}

Handler

• Closure• CurlHandler• CurlMultiHandler• StreamHandler• MockHandler

examples

Handlershould be callable$client = new Client([ 'handler' => 1]);

PHP Fatal error: Uncaught InvalidArgumentException: handler must be a callable

HandlerI implmented the type check

Huge Code Base

So lots of chances to contribute

Read Codeand

Make Contributions!

We are hiring!