dongyu1614 2010-05-16 22:08
浏览 73
已采纳

使用PHP cURL扩展在两个网站之间共享相同的cookie

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. 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.

The following code does not work:

$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);
  • 写回答

1条回答 默认 最新

  • douyi9787 2010-05-16 22:50
    关注

    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">
    

    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.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?