Guzzle 'on_headers' Request

390 views
Skip to first unread message

Dev By Dylan

unread,
Dec 28, 2016, 11:19:28 AM12/28/16
to Guzzle - PHP HTTP client and REST client framework
I seem to be having trouble figuring out how to run 3 sequential async requests, the first 2 should complete at the 'on_headers' function, and the last should execute normally. However, I also would like to use my cookie jar while adding session cookies picked up along the way. Is there any way to perform a guzzle request that does not download the body, but isn't a HEAD request, and uses the cookies from the previous request? Thanks all.

use GuzzleHttp\Client;

$jar = new \GuzzleHttp\Cookie\CookieJar();
$base_headers = ['Accept-Encoding' => 'gzip, deflate, br', 'Cache-Control' => 'no-cache', 'Accept' => '*/*'];
$client = new \GuzzleHttp\Client(array( 
'verify' => false,
'cookies' => $jar,
'base_uri' => 'https://tmp.org',
'headers' => $base_headers, 
'decode_content' => false,
'allow_redirects' => false
));

$res1 = $client->post('pkmslogin.form', ['form_params' => ['login-form-type' => 'pwd', 'username' => 'testuser', 'password' => 'testpass']]);
$res2 = $client->head('ESS/rp/0713/ESS', []);
$res3 = $client->post('ESS/data/ess/services/TnA.asmx/GetUpcomingShifts?siteName=ess&siteId=1001134', ['decode_content' => true], '{}');
echo $res3->getBody();


Artur Bodera

unread,
Dec 28, 2016, 4:37:10 PM12/28/16
to Guzzle - PHP HTTP client and REST client framework
"Sequential" is the opposite of async. If you did async (pool) then requests and responses could be out of order and you can't guarantee you'll have session cookies in the Jar. I think what you need is just regular sync (blocking) request for login, which updated the cookie jar. Afterwards, you can send any number of async requests using the same cookie jar. For not-downloading, scan the docs for body stream, AFAIR that should be it.

--


You received this message because you are subscribed to the Google Groups "Guzzle - PHP HTTP client and REST client framework" group.


To unsubscribe from this group and stop receiving emails from it, send an email to guzzle+un...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.


--

Dev By Dylan

unread,
Dec 28, 2016, 6:07:10 PM12/28/16
to Guzzle - PHP HTTP client and REST client framework
Ah. I understand the difference, but sometimes get them mixed up. Thank you for clarifying. Also thanks for the stream suggestion, however when I try that, it takes about 50% longer for the script to compete. Any insight as to why, as well as how I could cut down the time? Thanks again. Here is the script I'm currently working with...

<?php

$jar = new \GuzzleHttp\Cookie\CookieJar();
$client = new \GuzzleHttp\Client(array( 
'verify' => false,
'cookies' => $jar,
'base_uri' => 'https://tmp.org',
//'handler' => $stack,
'headers' => ['Accept-Encoding' => 'gzip'],
'decode_content' => false,
'stream' => true
));

$res1 = $client->post('pkmslogin.form', ['form_params' => ['login-form-type' => 'pwd', 'username' => 'tmpuser', 'password' => 'tmppass']]);
$res2 = $client->get('ESS/rp/0713/ESS', []);
$res3 = $client->post('ESS/data/ess/services/TnA.asmx/GetUpcomingShifts?siteName=ess&siteId=1001134', ['decode_content' => true, 'stream' => false], '{}');
echo $res3->getBody();

?>

Artur Bodera

unread,
Dec 28, 2016, 11:02:22 PM12/28/16
to Guzzle - PHP HTTP client and REST client framework
I'd leave stream enabled and then just refrain from seeking/accessing the steam. That would prevent loading too much of the response data. You could play with range header or take a look at guzzle source to see if there's a way to prevent any downloads. It might be possible via some curl flag or middleware method that'd return earlier, but I can't remember now.
Message has been deleted

Dev By Dylan

unread,
Dec 29, 2016, 1:04:45 AM12/29/16
to Guzzle - PHP HTTP client and REST client framework
Thanks again, I'll look into any middleware. I believe there's no curl flag that will solve all my problems here, the closest thing would be CURLOPT_NOBODY, but that just converts my request from a get/post to a head. This is no good because many servers supply different responses to different methods, this being no exception.

As for sticking to the stream and not seeking any data from the response:
That's what I believe I have done in the code from the previous post (my 2nd post on this topic) but again, when I compare the speed to the exact same script with and without the stream option set, when stream is disabled it runs approx 1 second, with streaming enabled (on the 1st 2 resources) it takes 1.5 seconds on average. Why the bump in response time, even though you can see in my code that I'm not even accessing those streams?

Artur Bodera

unread,
Dec 29, 2016, 2:40:57 AM12/29/16
to Guzzle - PHP HTTP client and REST client framework
Not sure Dylan. Would profile it quickly to find out. AFAIR disabling stream will just make guzzle wait and load the whole response from server and store it in a string, that's why you'd get longer execution times.

Dev By Dylan

unread,
Dec 29, 2016, 12:31:41 PM12/29/16
to Guzzle - PHP HTTP client and REST client framework
I appreciate your time Arthur!

But what's getting me, that what I'm saying after a full day of testing it seems like using functions like "on_headers" or the option "'stream' => true" both seem to actually take longer to execute even though I am not reading any data from their responses. I'm saying when I add stream => true, it takes longer, not shorter. And you can see in my code that I'm not reading the data.

So I've tried with stream and shown the outcome, here's when  I tried the on_headers approach, also which took longer than the traditional requests...

<?php

$jar = new \GuzzleHttp\Cookie\CookieJar();
$client = new \GuzzleHttp\Client(array( 
'verify' => false,
'cookies' => $jar,
'base_uri' => 'https://oasis-sso.tmp.org',
'headers' => ['Accept-Encoding' => 'gzip'], 
'decode_content' => false
));
echo '::1send::' . round(microtime(true) * 1000);
$client->post('pkmslogin.form', ['form_params' => ['login-form-type' => 'pwd', 'username' => 'tmpuser', 'password' => 'tmppass'], 'on_headers' => function (\Psr\Http\Message\ResponseInterface $res1) { 
    global $client, $jar;
    extractCookies($res1, '.tmp.org', $jar);
    $client->get('ESS/rp/0713/ESS', ['on_headers' => function (\Psr\Http\Message\ResponseInterface $res2) { 
global $client, $jar;
extractCookies($res2, '.publix.org', $jar);
$res3 = $client->post('ESS/data/ess/services/TnA.asmx/GetUpcomingShifts?siteName=ess&siteId=1001134', ['decode_content' => true], '{}');
echo $res3->getBody();
}]);
}]);


function extractCookies($response, $host, $jar) {
if ($cookieHeader = $response->getHeader('Set-Cookie')) {
        foreach ($cookieHeader as $cookie) {
            $sc = GuzzleHttp\Cookie\SetCookie::fromString($cookie);
             if (!$sc->getDomain()) {
                    $sc->setDomain($host);
                }
            $jar->setCookie($sc);
        }
    }
}
?>


Artur Bodera

unread,
Dec 29, 2016, 11:02:58 PM12/29/16
to Guzzle - PHP HTTP client and REST client framework
You do call "echo $res3->getBody();" which would read the whole response and output it. 

Dev By Dylan

unread,
Dec 30, 2016, 5:46:53 AM12/30/16
to Guzzle - PHP HTTP client and REST client framework
I do, but if you notice in my OP I mentioned the first 2 requests should just return the head and the last one I need the entire contents. Also, you can see in my code that I have stream => false for the last page, and do not hook 'on_headers' for the last request in my other example indicting that I need the contents of the last page. And lastly, in my first post with I just used regular requests and retrieved all the contents of each file. My question remains though;

Why are these requests taking longer when I use stream => true, or the on_headers function, longer than just then normal get request even though I'm not reading the contents of the first 2 requests?

Artur Bodera

unread,
Dec 30, 2016, 8:49:17 PM12/30/16
to Guzzle - PHP HTTP client and REST client framework
1. Check if you're not getting different server response times with HEAD vs GET/POST
2. Upgrade curl
3. Profile it and see what takes longer


--
You received this message because you are subscribed to the Google Groups "Guzzle - PHP HTTP client and REST client framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guzzle+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages