【问题标题】:Enabling pecl_http request pool cookie persistence启用 pecl_http 请求池 cookie 持久性
【发布时间】:2011-06-15 20:55:46
【问题描述】:

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

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

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

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

谢谢!

【问题讨论】:

    标签: php http cookies pecl


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-11-05
      • 1970-01-01
      • 2016-04-24
      • 2013-03-06
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 2020-01-11
      相关资源
      最近更新 更多