2014-07-24 94 views
0

僅當我首先登錄時才能訪問虛構頁面www.randomDomain.com/onlyForRegisteredUsers。所以 我這樣做:如何在php中使用curl保持會話信息?

... 
curl_setopt($this->curl, CURLOPT_HEADER, false); 
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
curl_setopt($this->curl, CURLOPT_HTTPPROXYTUNNEL, true); 
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10); 
curl_setopt($this->curl, CURLOPT_TIMEOUT, 10); 
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->currentUseragent; 
curl_setopt($this->curl, CURLOPT_PROXY, $this->currentIP); 
curl_setopt($this->curl, CURLOPT_URL, $this->currentUrl); //www.randomDomain.com/login/ 
curl_setopt($this->curl, CURLOPT_POST, true); 
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->currentData); 
    //with login RandomUserName and pass ******* 
$page = curl_exec($this->curl); 

,併成功在$頁我已經得到的結果是這樣的:...<p>Welcome back, RandomUserName!</p>

如何保持有關會議的信息,我已經登錄,然後從www.randomDomain.com/onlyForRegisteredUsers閱讀內容?

回答

0

你可以這樣做:

$cookieFile = 'cookie.txt'; 
curl_setopt($this->curl, CURLOPT_COOKIESESSION, true); 
curl_setopt($this->curl, CURLOPT_COOKIEJAR, realpath($cookieFile)); 
curl_setopt($this->curl, CURLOPT_COOKIEFILE, realpath($cookieFile)); 

注:cookie文件必須存在,你可以創建文件,如果不存在具有:

if (!file_exists(realpath($cookieFile))) 
{ 
    touch($cookieFile); 
}