【发布时间】:2011-02-20 04:49:56
【问题描述】:
我想在我的 gmail 帐户中获取一些电子邮件的内容。我想使用 PHP cURL 扩展来做到这一点。我在第一次尝试时遵循了这些步骤:
- 在PHP代码中,输出https://www.google.com/accounts/ServiceLoginAuth的内容。
- 在浏览器中,用户输入用户名和密码登录。
- 在 PHP 代码中,将 cookie 保存在名为 cookie.txt 的文件中。
- 在 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