【问题标题】:share the same cookie between two website using PHP cURL extension使用 PHP cURL 扩展在两个网站之间共享相同的 cookie
【发布时间】:2011-02-20 04:49:56
【问题描述】:

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

  1. 在PHP代码中,输出https://www.google.com/accounts/ServiceLoginAuth的内容。
  2. 在浏览器中,用户输入用户名和密码登录。
  3. 在 PHP 代码中,将 cookie 保存在名为 cookie.txt 的文件中。
  4. 在 PHP 代码中,向https://mail.google.com/ 发送请求以及从 cookie.txt 检索到的 cookie 并输出内容。

以下代码不起作用:

$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);

【问题讨论】:

  • 为什么不起作用?会发生什么?
  • @Slaks:点击“登录”按钮后,浏览器离开本地主机,直接访问google.com/accounts/ServiceLoginAuth。因此,cookie.txt中没有保存任何有用的cookie

标签: php cookies curl


【解决方案1】:

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

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

登录详细信息将由浏览器提交到该页面,您的脚本永远不会获取 cookie。您需要向https://www.google.com/accounts/ServiceLoginAuth已经使用用户名和密码提交发布请求。只有这样 curl 才会收到带有 cookie 的响应。

也就是说,我建议你把所有这些都刮掉,在 GMail 中启用 IMAP 并使用它来访问你的电子邮件。

【讨论】:

  • 所以你的意思是我必须硬编码用户名和密码?
  • 而且我没有使用 IMAP,因为我可能需要访问其他谷歌服务,例如日历。
  • 没有。您可以显示带有用户名和密码字段的表单,其操作是您的电子邮件获取脚本。然后您将读取提交的数据并配置 curl 以将它们作为 POST 数据发送。
  • Google 拥有使用这些服务器的 API。您应该使用它们,这种方法可能会在 Google 更改其页面中的某些内容时中断。见code.google.com/apis/calendar/data/2.0/developers_guide.html
  • @Artefacto: 但我注意到google.com/accounts/ServiceLoginAuth 中的表单包含一些隐藏输入,其值是随机生成的,例如,
猜你喜欢
  • 2012-09-04
  • 2013-02-02
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
  • 2011-10-10
相关资源
最近更新 更多