使用PHP cURL扩展名在两个网站之间共享相同的cookie [英] share the same cookie between two website using PHP cURL extension

查看:110
本文介绍了使用PHP cURL扩展名在两个网站之间共享相同的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的gmail帐户中获取一些电子邮件的内容。我想使用PHP cURL扩展名来做到这一点。我第一次尝试遵循以下步骤:

I want to get the contents of some emails in my gmail account. I would like to use the PHP cURL extension to do this. I followed these steps in my first try:


  1. 在PHP代码中,输出 https://www.google.com/accounts/ServiceLoginAuth

  2. 在浏览器中,用户输入用户名和密码进行登录。

  3. 在PHP代码中,将cookie保存在名为cookie.txt的文件中。

  4. 在PHP代码中,发送请求到 https://mail.google.com/ 以及从cookie.txt中检索并输出的cookie

  1. In the PHP code, output the contents of https://www.google.com/accounts/ServiceLoginAuth.
  2. In the browser, the user input username and password to login.
  3. In the PHP code, save cookies in a file named cookie.txt.
  4. In the PHP code, send request to https://mail.google.com/ along with cookies retrieved from cookie.txt and output the contents.

以下代码不起作用:

$login_url = 'https://www.google.com/accounts/ServiceLoginAuth';
$gmail_url = 'https://mail.google.com/';
$cookie_file = dirname(__FILE__) . '/cookie.txt';

$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_URL, $login_url);
$output = curl_exec($ch);
echo $output;

curl_setopt($ch, CURLOPT_URL, $gmail_url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$output = curl_exec($ch);
echo $output;

curl_close($ch);


推荐答案

您的方法是错误的。您无法检索 https://www.google.com/accounts/ServiceLoginAuth 的内容,输出它,希望用户填写详细信息,然后按登录。由于该表单定义为

Your approach is wrong. You cannot retrieve the contents of https://www.google.com/accounts/ServiceLoginAuth and output it, expect the user to fill in the details and press login. Since the form is defined as

<form action="https://www.google.com/accounts/ServiceLoginAuth" method="post">

登录详细信息将由浏览器提交到该页面,并且您的脚本永远不会保留Cookie 。您需要向 https://www.google.com/accounts/ServiceLoginAuth 提交发布请求。 > 已使用用户名和密码。只有到那时curl才会收到Cookie的回复。

the login details will be submitted by the browser to that page and your script never get hold of the cookies. You need to submit a post request to https://www.google.com/accounts/ServiceLoginAuth already with the username and password. Only then curl will receive a response with the cookies.

那是说,我建议您抓取所有这些信息,在GMail中启用IMAP并使用它来访问您的邮件。

That said, I'd suggest you scrape all this, enable IMAP in GMail and use that to access your e-mails.

这篇关于使用PHP cURL扩展名在两个网站之间共享相同的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆