启用 pecl_http 请求池 cookie 持久性

发布于 2024-11-16 03:49:19 字数 448 浏览 1 评论 0原文

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

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

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

任何人都可以向我指出一个工作代码示例或应用程序,该示例或应用程序将多个 HttpRequest 对象发送到同一服务器并利用 pecl_http 的 cookie 持久性吗?

谢谢!

I'm trying to enable cookie persistence for a sent of pecl_http HttpRequest objects, sent using the same HttpRequestPool object (if it matters); unfortunately documentation is quite scarce, and despite all my attempts I do not think things work properly.

I have tried both using HttpRequestDataShare (albeit documentation here is very scarce) and using the 'cookiestore' request option to point to a file. I still do not see cookies sent back to the server(s) in consecutive requests.

To be clear, by "cookie persistence" I mean that cookies set by the server are automatically stored and re-sent by pecl_http on consecutive requests, without me having to manually handle that (if it comes to it I will, but I am hoping I don't have to do that).

Can anyone point me to a working code sample or application that sends multiple HttpRequest objects to the same server and utilizes pecl_http's cookie persistence?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多孤肩上扛 2024-11-23 03:49:19

请注意,请求池尝试并行发送所有请求,因此它们当然无法知道尚未收到的 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()));
}

Mind that the request pool tries to send all requests in parallel, so they cannot know cookies not yet received of course. 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()));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文