启用pecl_http请求池cookie持久性

时间:2011-06-15 20:55:46

标签: php http cookies pecl

我正在尝试为发送的pecl_http HttpRequest对象启用cookie持久性,使用相同的HttpRequestPool对象发送(如果重要);不幸的是,文档很少,尽管我尝试了所有的尝试,但我认为事情并不正常。

我尝试过使用HttpRequestDataShare(虽然这里的文档非常稀缺)并使用'cookiestore'请求选项指向文件。我仍然没有看到连续请求中的cookie被发送回服务器。

要明确的是,“cookie persistence”我的意思是服务器设置的cookie会自动存储并由pecl_http在连续的请求中重新发送,而我不必手动处理(如果它会发生,我会,但是我希望我不必这样做)。

有人能指出一个工作代码示例或应用程序,它将多个HttpRequest对象发送到同一台服务器并利用pecl_http的cookie持久性吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

请注意,请求池会尝试并行发送所有请求,因此他们无法知道当前尚未收到的Cookie。 E.g:

<?php

$url = "http://dev.iworks.at/ext-http/.cookie.php";

function cc($a) { return array_map("current", array_map("current", $a)); }

$single_req = new HttpRequest($url);

printf("1st single request cookies:\n");
$single_req->send();
print_r(cc($single_req->getResponseCookies()));

printf("waiting 1 second...\n");
sleep(1);

printf("2nd single request cookies:\n");
$single_req->send();
print_r(cc($single_req->getResponseCookies()));

printf("1st pooled request cookies:\n");
$pooled_req = new HttpRequestPool(new HttpRequest($url), new HttpRequest($url));
$pooled_req->send();
foreach ($pooled_req as $req) {
    print_r(cc($req->getResponseCookies()));
}

printf("waiting 1 second...\n");
sleep(1);

printf("2nd pooled request cookies:\n");
$pooled_req = new HttpRequestPool(new HttpRequest($url), new HttpRequest($url));
$pooled_req->send();
foreach ($pooled_req as $req) {
    print_r(cc($req->getResponseCookies()));
}

printf("waiting 1 second...\n");
sleep(1);

printf("now creating a request datashare\n");
$pooled_req = new HttpRequestPool(new HttpRequest($url), new HttpRequest($url));
$s = new HttpRequestDataShare();
$s->cookie = true;
foreach ($pooled_req as $req) {
    $s->attach($req);
}

printf("1st pooled request cookies:\n");
$pooled_req->send();
foreach ($pooled_req as $req) {
    print_r(cc($req->getResponseCookies()));
}

printf("waiting 1 second...\n");
sleep(1);

printf("2nd pooled request cookies:\n");
$pooled_req->send();
foreach ($pooled_req as $req) {
    print_r(cc($req->getResponseCookies()));
}