2015-10-21 1 views
1

Есть ли способ отправить несколько запросов с использованием промежуточного программного обеспечения и в соответствии с определенными параметрами?Отправить несколько запросов через промежуточное ПО в Guzzle

Чтобы быть более конкретным мне нужно:

class MyMiddleware 
{ 
    /** 
    * @param callable $handler 
    * @return \Closure 
    */ 
    public function __invoke(callable $handler) 
    { 
     return function (RequestInterface $request, array $options) use ($handler) { 
      if(!isset($options['token']){ 
       //request the token from a url 
      } 
      //after the token is aquired proceed with the original request 
      return $handler($request, $options); 
     }; 
    } 
} 

ответ

0

Это похоже на то, что вы спрашиваете о? Я сделал схожую с ним.

$handler = GuzzleHttp\HandlerStack::create(); 

$client = new GuzzleHttp\Client([ 
    'handler' = $handler, 
    // other options 
]) 

$handler->push(GuzzleHttp\Middleware::mapRequest(
    function (Psr\Http\Message\RequestInterface $request) use ($client) { 
     if ('POST' !== $request->getMethod()) { 
      return $request; 
     } 
     if($RequestIsOkAsIs) { 
      return $request; 
     } 

     $response = $client->get($someUrl, $requestOptions); 

     // Play with the response to modify the body 
     $newBody = 'results of stuff'; 

     return new GuzzleHttp\Psr7\Request(
      $request->getMethod(), 
      $request->getUri(), 
      $request->getHeaders(), 
      $newBody, 
      $request->getProtocolVersion() 
     ); 
    } 
); 
Смежные вопросы