我满怀希望解决我的问题来到这里。我有这个网站,我想登录并访问成员(member)专享页面。所以基本上是使用 cURL 的 php 代码登录,然后打开一个仅限成员(member)的页面。下面是我使用的代码。但是每次我尝试访问仅限成员(member)的页面时,它都会要求我登录。所以它不会保存在我已经登录的 session 或 cookie 中。
..........
set_time_limit(0);
$username = 'myemail@yahoo.com';
$password = 'mypassword';
$loginUrl = 'http://radaris.com/login/a.login';
$cookie_file_path = getcwd() . '/tmp/cookie.txt';
//init curl
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 0);
//Set the post parameters
curl_setopt($ch, CURLOPT_HEADER, 1); // set to 0 to eliminate header info from response
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email='.$username.'&password='.$password."&remember_me=1");
//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request (the login)
$store = curl_exec($ch);
echo "<font color=red>Login</font><br>".$store."<br><br>";
$postfields = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://radaris.com/my/');
curl_setopt($ch, CURLOPT_HEADER, 1); // set to 0 to eliminate header info from response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
//Handle cookies for the login
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
echo "<font color=red>Report</font><br>".$result."<br><br>";
..........
我得到的结果是:
Login HTTP/1.1 200 OK Server: nginx Date: Thu, 12 Jun 2014 14:21:01 GMT Content-Type: application/json Transfer-Encoding: chunked Connection: keep-alive Keep-Alive: timeout=20 Cache-Control: no-store, no-cache, must-revalidate Cache-Control: post-check=0, pre-check=0 Expires: Wed, 14 Jan 2009 19:00:00 GMT X-Robots-Tag: noindex {"/Rdf.showError":[["Invalid email or password","login_form"],["Check your email","login_email"],["Check your password","login_password"]]}
Report HTTP/1.1 302 Found Server: nginx Date: Thu, 12 Jun 2014 14:21:03 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Keep-Alive: timeout=20 Location: /login?backurl=%2Fmy%2F HTTP/1.1 200 OK Server: nginx Date: Thu, 12 Jun 2014 14:21:03 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Keep-Alive: timeout=20 Vary: Accept-Encoding Vary: Accept-Encoding Set-Cookie: PHPSESSID=qdg80d48dd17hl879h86v9ruo5; path=/; HttpOnly Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Login We never share or disclose your information!
以上仅为标题。在第二个标题中,我再次获得登录表单以登录,这意味着我的代码第一位中的成功登录没有保存 session ,这就是为什么当我访问仅限成员(member)的页面时,它要求登录。
我真的很想把这件事做好。提前谢谢大家。
最佳答案
您正在使用两个单独的 curl 句柄,并且您从未在第二个中设置 cookie 内容。每个 curl 句柄彼此完全独立,因此如果您不将您在第一个句柄中创建的 cookiejar/cookiefile 告诉第二个句柄,您就永远不会真正使用从登录请求中获得的 cookie。
从技术上讲,您不必对每个句柄执行一个请求。一个句柄可以执行多个请求。唯一可能更容易开始新的情况是,如果您提出了一个复杂的自定义请求并设置了许多选项,那么重新开始而不是为下一个请求重新设置这些选项会更容易。
所以你的代码序列应该是:
init curl
set up cookiefile/cookiefar and other options
do login request
do next next request
close curl
如果您不想重写,那么至少将 cookiejar/cookiefile 选项添加到第二个请求。
关于PHP cURL 登录站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24186878/