2015-08-07 15 views
5

Tôi đang cố gắng để cấu hình các yêu cầu được thực hiện cho một máy chủ API từ một máy khách PHP bằng cách sử dụng Guzzle (v 6).Làm cách nào để hồ sơ yêu cầu Guzzle 6?

Trong Guzzle 5.3, hãy xử lý sự kiện completebefore này.

class GuzzleProfiler implements SubscriberInterface 
{ 
    public function getEvents() 
    { 
     return [ 
      'before' => ['onBefore'], 
      'complete' => ['onComplete'] 
     ]; 
    } 

    public function onBefore(BeforeEvent $event, $name) 
    { 
     start_profiling(); 
    } 

    public function onComplete(CompleteEvent $event, $name) 
    { 
     end_profiling(); 
    } 
} 

Nhưng làm cách nào để thực hiện điều này trong v6?

Trả lời

1

Chỉ tìm thấy nó bằng Middleware. Đây là mã.

class Profiler { 

    /** 
    * @return callable 
    */ 
    public static function profile() { 
     return function(callable $handler) { 
      return function(\Psr\Http\Message\RequestInterface $request, array $options) use ($handler) { 
       start_profiling(); 
       return $handler($request, $options)->then(function(\Psr\Http\Message\ResponseInterface $response) use ($token) { 
        end_profiling(); 
        return $response; 
       }); 
      }; 
     }; 
    } 
} 

Và sau đó đính kèm profiler như thế này.

$stack = \GuzzleHttp\HandlerStack::create(); 
$stack->push(Profiler::profile()); 
$client = new \GuzzleHttp\Client([ 
    'handler' => $stack 
]); 
Các vấn đề liên quan