2013-07-11 51 views
0

您好,我嘗試將cookie存儲在txt文件中時出現問題。 my_cookies.txt是該文件的同一文件夾中的一個txt文件。使用cUrl管理cookie

我想登錄前後管理的cookie,在這個例子中的Facebook 然後我想插入的cookie數據庫中的,但首先我需要把它放在一個.txt或至少保存

顯然,電子郵件和密碼不要那些。 =)

<?php 

/* STEP 2. define path of form_action */ 
$url = "https://facebook.com/login.php?login_attempt=1"; 
$email = "nn"; 
$password = "nn"; 

/* STEP 3. create a connection with curl, give post information and save cookies */ 
$handler = curl_init(); 
//insert in the handler the path 
curl_setopt($handler, CURLOPT_URL, $url); 

//insert in the handler parameters of post 
curl_setopt($handler, CURLOPT_POSTFIELDS,'email='.urlencode($email).'&pass='.urlencode($password)); 

//insert in the handler option to allow sending post information 
curl_setopt($handler, CURLOPT_POST,1); 

curl_setopt($handler, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false); 

//load and save cookies generates in temp file 
curl_setopt($handler, CURLOPT_COOKIEJAR, "my_cookies.txt"); 
curl_setopt($handler, CURLOPT_COOKIEFILE, "my_cookies.txt"); 

//catch information 
curl_setopt ($handler, CURLOPT_RETURNTRANSFER, true); 

//define agent 
curl_setopt($handler, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"); 

$response = curl_exec ($handler); 

curl_close($handler); 

echo $response; 
?> 

感謝您的幫助=),我希望你可以做任何事情來幫助我:(

回答

1

PHP 5.3+

curl_setopt($handler, CURLOPT_COOKIEJAR, __DIR__."/my_cookies.txt"); 
curl_setopt($handler, CURLOPT_COOKIEFILE, __DIR__."/my_cookies.txt"); 

PHP 5.2-

curl_setopt($handler, CURLOPT_COOKIEJAR, dirname(__FILE__)."/my_cookies.txt"); 
curl_setopt($handler, CURLOPT_COOKIEFILE, dirname(__FILE__)."/my_cookies.txt"); 

還可以使用的文件絕對路徑。 __DIR__給你那個力量!

+0

謝謝你的回答。 我試着用__DIR__但系統不保存我的txt文件中的信息。 你還有別的想法嗎? =) – GDedieu92

+0

我需要的是保存所有cookie,當我發送請求時,某些域響應我 – GDedieu92

+0

@ GDedieu92如果您設置Cookie Jar /文件路徑正確,cURL將爲您存儲Cookie。您可以打開文件並查看。看看它是否包含你期望的。 – CodeAngry

-1

您可以將餅乾到$cookies變量和使用,在地方,

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

然後曲奇工作是在捲曲過程完成之後。你可以做任何你想要的變量($cookies)。

提示:

  1. 對於存儲到.TXT - 你__FILE__存儲相同的目錄或追加__FILE__更多的路徑在其他目錄來存放。
  2. 您可以使用md5($_SERVER['REMOTE_ADDR'])進行生成單獨使用.txt
  3. 對於存儲到數據庫中,INSERT命令可以。