2011-06-15 40 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())); 
}