0

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

我已经尝试过使用 HttpRequestDataShare(尽管这里的文档非常稀缺)和使用 'cookiestore' 请求选项来指向一个文件。我仍然没有看到在连续请求中发送回服务器的 cookie。

明确地说,“cookie持久性”是指服务器设置的cookie会自动存储并由pecl_http在连续请求时重新发送,而无需我手动处理(如果涉及到它,我会的,但我希望我不必这样做)。

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

谢谢!

4

1 回答 1

0

请注意,请求池会尝试并行发送所有请求,因此他们当然无法知道尚未收到的 cookie。例如:

<?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()));
}
于 2011-06-16T09:59:07.890 回答