2013-07-11 52 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命令可以。