[英]Guzzle hangs Apache when passing PHPSESSID session cookie
I am using Guzzle get request in one PHP script to fetch another. 我在一个PHP脚本中使用Guzzle get请求来获取另一个。
$guzzle->get("urltomysecondpage")->getBody();
However, Guzzle is not aware of the session ID cookie, so my target script cannot access session variables. 但是,Guzzle不知道会话ID cookie,因此我的目标脚本无法访问会话变量。
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. 文档指出,我要做的只是将“ cookies”设置为“ true”,并且Guzzle将使用调用脚本看到的相同cookie集。
$guzzle->get("urltomysecondpage", ['cookies' => TRUE])->getBody();
However, this simply doesn't work. 但是,这根本行不通。 The target script sees no cookies.
目标脚本看不到cookie。 Documentation also allows to explicitly set an array of cookies like this:
文档还允许显式设置Cookie数组,如下所示:
$guzzle->get("urltomysecondpage", ['cookies' => ['PHPSESSID'=>$_COOKIE['PHPSESSID']]])->getBody();
It works, but not if I specify one of the cookies to have the key "PHPSESSID". 它有效,但是如果我指定其中一个cookie具有键“ PHPSESSID”,则无效。 If I do, the Apache server hangs completely.
如果这样做,Apache服务器将完全挂起。 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. 使用
cookies => true
选项时遇到一些麻烦。 have you tried providing a default CooieJar
? 您是否尝试提供默认的
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. 现在,默认情况下,该客户端的每个请求都设置了相同的Cookie罐。
The webserver isn't able to open the same session for 2 requests. 网络服务器无法为2个请求打开相同的会话。 To fix this you can first close the session and then reopen it after you've made the request with Guzzle.
要解决此问题,您可以先关闭会话,然后在通过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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.