What is the difference between
guzzle/guzzle
and
guzzlehttp/guzzle?
FOSHttpCache needs to send HTTP requests to varnish for cache invalidation
=> Márk Sági-Kazár
How to create an object without knowing the class?
1 | interface RequestFactoryInterface |
2 | { |
3 | function createRequest(string $method , $uri ) |
4 | : RequestInterface; |
5 | } |
=> Woody Gilk
How to send that request?
1 | interface ClientInterface |
2 | { |
3 | function sendRequest(RequestInterface $request ) |
4 | : ResponseInterface; |
5 | } |
=> Tobias Nyholm
1 | sendRequest(RequestInterface $request , $config ) |
What about the cute elephant?
1 | public function handleRequest( |
2 | RequestInterface $request , |
3 | callable $next , |
4 | callable $first |
5 | ): Promise; |
1 | CachePlugin::clientCache - max-age, no-cache |
2 | CachePlugin::serverCache - additionally: private |
1 | /** |
2 | * @param RequestInterface[] The requests to send |
3 | * |
4 | * @throws BatchException If any request threw |
5 | */ |
6 | public function sendRequests( |
7 | array $requests |
8 | ): BatchResult |
1 | public function get( $uri , array $headers = []): Re |
2 |
3 | public function post( $uri , array $headers = [], $b |
4 |
5 | public function put( $uri , array $headers = [], $bo |
6 |
7 | public function send(string $method , $uri , array $ |
01 | httplug: |
02 | clients: |
03 | app: |
04 | http_methods_client: true |
05 | plugins: |
06 | - header_defaults: |
07 | headers: |
08 | "X-Conference": "Benelux" |
09 | - header_set: |
10 | headers: |
11 | "User-Agent": "PHP/Symfony" |
01 | /** @var HttpMethodsClient */ |
02 | private $httpClient ; |
03 |
04 | public function status(): Response { |
05 | try { |
06 | $r = $this ->httpClient->get( 'http://php.net/' ); |
07 | } catch (Exception $e ) { |
08 | return new Response( 'Failed' , 502); |
09 | } |
10 | return new Response(200 === $r ->getStatusCode() |
11 | ? 'Success' |
12 | : 'Error' ); |
1 | sendAsync(RequestInterface $request ): Promise; |
2 | Promise::wait(): ResponseInterface; |
01 | interface Promise |
02 | { |
03 | const PENDING = 'pending' ; |
04 | const FULFILLED = 'fulfilled' ; |
05 | const REJECTED = 'rejected' ; |
06 |
07 | public function then(?callable $onFulfilled , |
08 | public function getState(); |
09 | public function wait( $unwrap = true); |
10 | } |
01 | try { |
02 | $promises = []; |
03 | foreach ( $uris as $u ) { |
04 | $promises [ $u ] = $httpClient ->sendAsyncRequest( |
05 | $requestFactory ->createRequest( 'GET' , $u ) |
06 | ); |
07 | } |
08 | } catch (\Exception $e ) { |
09 | return new Response( 'Configuration error' , 500); |
10 | } |
01 | foreach ( $promises as $uri => $promise ) { |
02 | $s .= $uri . ':' ; |
03 | try { |
04 | $r = $promise ->wait(); |
05 | if (200 === $r ->getStatusCode()) { |
06 | $s .= 'Up and running' ; |
07 | } else { |
08 | $s .= 'Error: ' . $r ->getStatusCode(); |
09 | } |
10 | } catch (\Exception $e ) { |
11 | $s .= 'Network Error: ' . $e ->getMessage(); |
12 | } |
13 | } |
01 | $results = []; |
02 | $promises = []; |
03 | foreach ( $uris as $u ) { |
04 | $promise = $httpClient ->sendAsyncRequest( |
05 | $requestFactory ->createRequest( 'GET' , $u )); |
06 | $onFulfilled = function (ResponseInterface $r ) |
07 | use ( $u , $results ) { |
08 | if (200 === $r ->getStatusCode()) { |
09 | $results [ $u ] = 'Up and running' ; |
10 | } else { |
11 | $results [ $u ] = 'Error: ' . $r ->getStatusCode(); |
12 | } |
13 | }; |
14 | } |
01 | ... |
02 | $onRejected = function (Exception $r ) |
03 | use ( $u , $results ) { |
04 | $results [ $u ] = 'Error: ' . $e ->getMessage(); |
05 | }; |
06 | $promise ->then( $onFulfilled , $onRejected ); |
07 | $promises [] = $promise ; |
08 | } |
09 | } catch (Exception $e ) { |
10 | return new Response( 'Configuration error' , 500); |
11 | } |
1 | foreach ( $promises as $promise ) { |
2 | $promise ->wait(false); |
3 | } |
4 | foreach ( $results as $u => $text ) { |
5 | $s .= $uri . ':' .text; |
6 | } |