2011-06-15 39 views
0

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

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

通过“cookie持久性”,我的意思是由服务器设置的cookie自动存储并通过连续请求pecl_http重新发送,而不必手动处理(如果涉及到,我会,但我希望我不必这样做)。

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

谢谢!

回答

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())); 
}