2013-06-18 127 views
2

我有我的捲曲請求的問題。登錄與捲曲並與捲曲的其他請求之後

我想登錄,它的工作原理。我想保存cookie以保持連接可用,它工作。

$lien = 'https://thewebsite.com'; 
$postfields = array(
    'username' => 'test123', 
    'password' => 'test123' 
); 

$path_cookie = 'connexion.txt'; 
if (!file_exists(realpath($path_cookie))) touch($path_cookie); 

$curl = curl_init(); 

curl_setopt($curl, CURLOPT_URL, $lien); 
curl_setopt($curl, CURLOPT_COOKIESESSION, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields); 
curl_setopt($curl, CURLOPT_COOKIEJAR, realpath($path_cookie)); 

$return = curl_exec($curl); 

echo($return); 
curl_close($curl); 

第二部分:

$lien2 = 'https://thewebsite.com/myaccount'; 

$curl = curl_init(); 

curl_setopt($curl, CURLOPT_URL, $lien2); 
curl_setopt($curl, CURLOPT_COOKIESESSION, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields); 
curl_setopt($curl, CURLOPT_COOKIEFILE, realpath($path_cookie)); 

$return = curl_exec($curl); 

echo realpath($path_cookie); 
curl_close($curl); 

但是,當我要做出其他要求,將無法正常工作,輸出:

對象移動到這裏。

這裏是登錄(https://thewebsite.com)的網頁...

所以聯接不留可用,並且服務器已被踢出當我嘗試實現第二curl命令。

任何人都可以幫助我嗎?

也許第一個請求是不完整的在第二個之前,我怎麼能做出2個請求之間的暫停? (睡眠將無法工作)

+0

也許第一個請求執行第二時是不完整的...睡眠(10)將無法正常工作,使2之間的「暫停」,有許多人有一個想法? – Peter

+0

或者,也許會不「開」當我嘗試第二請求...... – Peter

回答

2

php documentation摘自:

CURLOPT_COOKIESESSION:

TRUE這個標記爲新的cookie 「會話」。它將強制libcurl忽略所有即將加載來自前一個會話的「會話cookie」的cookie。默認情況下,libcurl總是存儲和加載所有的cookie,如果它們是會話cookie,則獨立。會話cookie是沒有失效日期的cookies,它們只是爲了這個「會話」而存在並存在。

所以,換句話說,從second part代碼中刪除CURLOPT_COOKIESESSION,你的代碼應該工作。

+0

非常感謝,它像一個魅力的工作:) – Peter