PHP:获取需要身份验证的远程网页的内容?

时间:2010-05-16 06:50:55

标签: php

我收到许多内容相同的电子邮件,例如

Name: XXX
Phone: XXX
Bio: XXX
......

我想编写一个PHP脚本来解析这些电子邮件并将数据保存到数据库中。我尝试使用file_get_contents()来获取这些电子邮件的内容。

问题是,它需要身份验证才能访问我的电子邮件。即使我已经在服务器上登录了我的电子邮件帐户(实际上是localhost),file_get_contents()仍会返回提示我登录的网页。

那么如何使用PHP脚本访问我的电子邮件内容?

编辑:这是一个Gmail帐户 以下代码不起作用。

$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_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);

curl_setopt($ch, CURLOPT_URL, $gmail_url);
// not sure how to set this option
curl_setopt($ch, CURLOPT_REFERER, $login_url);

$output = curl_exec($ch);
curl_close($ch);

echo $output;

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

答案 2 :(得分:0)

我怀疑您正在尝试使用https访问安全协议(file_get_contents)。为此,您应该在php.ini文件中取消注释身份验证和openssl扩展名。否则file_get_contents会引发错误,无法找到文件/网址。