Upload
dqneo
View
370
Download
0
Embed Size (px)
Citation preview
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
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
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;}
Handlershould be callable$client = new Client([ 'handler' => 1]);
PHP Fatal error: Uncaught InvalidArgumentException: handler must be a callable