PHP: get the contents of a remote webpage requiring authentication?

I get a lot of emails whose contents have the same template, for example.

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

I want to write a PHP script to parse these letters and store data in a database. I tried using the get_contents () file to get the contents of these emails.

The problem is that authentication is required to access my emails. Although I signed my email account on the server (actually localhost), file_get_contents () will still return a web page that prompts me to login.

So how do I access the contents of my emails using a PHP script?

EDIT: This is a gmail account.
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_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; 
+4
source share
3 answers

I suspect you are trying to access a secure protocol ( https ) using file_get_contents . For this to work, you must have authentication and the openssl extension is uncommented in the php.ini file. Otherwise, file_get_contents will cause an error not to find the file / url.

0
source

Source: https://habr.com/ru/post/1309895/


All Articles