0

您好,我在尝试将 cookie 存储在 txt 文件中时遇到问题。my_cookies.txt 是该文件同一文件夹中的一个 txt。

我想在登录之前和之后管理 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; 
?>

感谢您的帮助=),我希望您可以做任何事情来帮助我:(

4

3 回答 3

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__给你那种力量!

于 2013-07-11T22:36:53.937 回答
1

facebook 提供了一个 API,它比使用 curl 随机 facebook 页面更不容易出错

要回答您的实际问题,尽管 这是您正在寻找的函数, 它会将文件读入一个变量,然后您可以将其存储

于 2013-07-11T22:26:28.467 回答
-1

您可以将 cookie 分配给$cookies变量并就地使用它,

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

然后在 cURL 过程中完成 cookie 的工作之后。你可以对那个变量()做任何你想做的事情$cookies

小费:

  1. 对于存储到 .txt - 您__FILE__存储相同的目录或附加__FILE__更多路径以存储在另一个目录中。
  2. 您可以md5($_SERVER['REMOTE_ADDR'])用于生成单独使用的 .txt
  3. 要存储到数据库中,INSERT命令就可以了。
于 2013-07-16T09:06:46.020 回答