2012-03-17 191 views
1

我想知道是否可以在mysql中存儲curl的會話cookie。在mysql中存儲curl會話cookie

curl_setopt($ch, CURLOPT_COOKIEJAR, get_the_cookie()); 
curl_setopt($ch, CURLOPT_COOKIEFILE, get_the_cookie()); 

get_the_cookie()返回一個唯一到user.txt文件路徑, 但我希望能夠給這個cookie存儲在MySQL而不是文件系統,如果它甚至有可能。

回答

4

cURL不允許您直接執行此操作,但可以通過在請求期間創建臨時文件並根據需要手動將其內容傳送到數據庫來僞造它。

例如:

$cookiejar = // get cookies from database 
$cookiejarfile = tempnam(sys_get_temp_dir()); 
$cookiefile = tempnam(sys_get_temp_dir()); 
file_put_contents($cookiejarfile, $cookiejar); 

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejarfile); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); 

$newcookies = file_get_contents($cookiefile); 

// and now save cookies to database and clean up temp files