Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] how can I get total, connection time et redirect count from guzzle #1091

Closed
omansour opened this issue May 28, 2015 · 9 comments
Closed

Comments

@omansour
Copy link

typically CURLINFO_TOTAL_TIME, CURLINFO_CONNECT_TIME and CURLINFO_REDIRECT_COUNT ?

thanks for all and thanks for the great lib !

@mtdowling
Copy link
Member

I don't want to subclass ResponseInterface to add any new methods, but I was considering add a callback for handler specific context information, but haven't spec'd anything out yet.

I was thinking it would be a new request option that you provide a callback to and the callback is invoked with an associative array of custom transfer information (like this cURL stuff).

@omansour
Copy link
Author

omansour commented Jun 1, 2015

hey @mtdowling

nice to read you, great fan of your work.

Having the infos in the response object will be easier for the user to deal with. But the callback option will work I guess, hydrating a sort of parameters bag with the cURL options in the keys. Sadly, I cant see in the new guzzle where this can be implemented.

@mtdowling
Copy link
Member

One other reason why I like a callback more is that sending a single request using the client's send method could in fact trigger multiple HTTP requests to be sent (e.g., redirects, cache validations, etc.). By using a callback, you can receive all of the stats for each request and does not require consumers to rely on a specific concrete implementation of ResponseInterface that adds custom methods.

@puckbag
Copy link

puckbag commented Jun 11, 2015

I'm building an uptime tool and want to record connection and response times. Extending the CurlFactory and attaching the info to the response object worked for me. My first experience with Guzzle...very nice!

<?php

require __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlFactory;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Handler\EasyHandle;

class MyCurlFactory extends CurlFactory {
    public function release(EasyHandle $easy)
    {
        if ($easy->response)
        {
            $easy->response->curl_info = curl_getinfo($easy->handle);
        }
        return parent::release($easy);
    }
}

$handler = new CurlHandler([
    'handle_factory' => new MyCurlFactory(3),
]);
$stack = HandlerStack::create($handler);
$client = new Client(['handler' => $stack]);

$res = $client->get('https://www.example.com/');
print_r($res->curl_info);

Here's a more complete example using a custom choose_handler for Multi as well:
https://gist.github.com/puckbag/c49f0cbe19574942dd17

@mtdowling
Copy link
Member

@puckbag Nice work!

@omansour
Copy link
Author

@puckbag excellent :) so nothing to do in guzzle itself regarding this specific need !

@mickgeek
Copy link

@puckbag 👍

@bilibvivo
Copy link

You can use Event System. this is a example:

$mySubscriber = new MySubscriber();
$HttpClient = new \GuzzleHttp\Client;
$HttpClient->getEmitter()->attach($mySubscriber);
$result = $HttpClient->get('http://www.github.com/');
$result = $result->getStatusCode();

AND MySubscriber Class:

use GuzzleHttp\Event\EmitterInterface;
use GuzzleHttp\Event\SubscriberInterface;
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\CompleteEvent;

class MySubscriber implements SubscriberInterface
{
    public function getEvents()
    {
        return [
            // Provide name and optional priority
            'before'   => ['onBefore', 100],
            'complete' => ['onComplete'],
            // You can pass a list of listeners with different priorities
            //'error'    => [['beforeError', 'first'], ['afterError', 'last']]
        ];
    }

    public function onBefore(BeforeEvent $event, $name)
    {
        $event->getRequest()->addHeaders(['sign'=>'xxx']);
    }

    public function onComplete(CompleteEvent $event, $name)
    {
        $curl_info = $event->getTransferInfo();
        print_r($curl_info);
    }
}

docs link: http://docs.guzzlephp.org/en/5.3/events.html#

@sagikazarmark
Copy link
Member

Nice, worth noting that this is for Guzzle 5.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants