简体   繁体   中英

Guzzle hangs Apache when passing PHPSESSID session cookie

I am using Guzzle get request in one PHP script to fetch another.

$guzzle->get("urltomysecondpage")->getBody();

However, Guzzle is not aware of the session ID cookie, so my target script cannot access session variables.

Documentation states that all I have to do is set "cookies" to "true" and Guzzle shall use the same cookie set that the calling script sees.

$guzzle->get("urltomysecondpage", ['cookies' => TRUE])->getBody();

However, this simply doesn't work. The target script sees no cookies. Documentation also allows to explicitly set an array of cookies like this:

$guzzle->get("urltomysecondpage", ['cookies' => ['PHPSESSID'=>$_COOKIE['PHPSESSID']]])->getBody();

It works, but not if I specify one of the cookies to have the key "PHPSESSID". If I do, the Apache server hangs completely. Not only the current request, but it also stops responding to all requests.

What's going on?

I had some trouble using the cookies => true option. have you tried providing a default CooieJar ?

Example:

$jar = new GuzzleHttp\Cookie\CookieJar();
$default = [
    "cookies" => $jar
];
$c = new GuzzleHttp\Client(["defaults" => $default]);

The same cookie jar is now set by default to each request made by that client.

The webserver isn't able to open the same session for 2 requests. To fix this you can first close the session and then reopen it after you've made the request with Guzzle.

Example:

session_start();

$sessionId = session_id();

session_write_close();

$client = new Client(
    array(
        'base_uri' => 'http://www.yoursite.tld',
        'cookies' => false,
    )
);

$cookie = new GuzzleHttp\Cookie\SetCookie();
$cookie->setName('PHPSESSID');
$cookie->setValue($sessionId);
$cookie->setDomain('www.yoursite.tld');

$cookieJar = new CookieJar(
    false,
    array(
        $cookie,
    )
);

$response = $client->request(
    'GET',
    '/urltomysecondpage',
    array(
        'cookies' => $cookieJar,
    )
);

session_start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM