2010-05-16 52 views
4

我收到许多内容相同的电子邮件,例如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; 

回答

0

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

+0

是的,这是HTTPS protocal。 openssl扩展已经启用。 – powerboy 2010-05-16 06:59:52

+0

@powerboy:你在使用IIS服务器吗? – Sarfraz 2010-05-16 07:14:31

+0

@Sarfraz:不,它是windows上的apache – powerboy 2010-05-16 07:15:56

1
+0

我一定会尝试libgmailer。但我很好奇它如何做到这一点?我的意思是,它如何获得需要验证的远程网页的内容? – powerboy 2010-05-16 07:07:10

+0

@powerboy:您可以发送cookie给它。 现在,我从来没有真正搞砸GMail,但我认为你可以直接设置他期待的正确的cookie并连接到收件箱页面。 你可以用'fsockopen'或cURL来做到这一点。 – nico 2010-05-16 07:35:19