How to access gmail mail id using curl in php
Step 1 : first please create your client ID and client secrete key using below given url.
Step 2 : https://developers.google.com/gmail/
Step 3 : you will get client id and secrete key from there and call back url.
Step 4 : After all this Please copy this code and paste your file and put client id, secrete key and call back url.
Gmail Credential Page :
<?php $client_id = Put your client Id; // '**************0gd4lvravm92ui60d6pl.apps.googleusercontent.com'; $client_secret = Put your gmail secret; '654646464646QgcsMiv_I'; $redirect_uri = Put your callback URL; $max_results = 500; //set the Maximum result you want to fetch $auth_code=''; if(isset($_GET["code"])) { $auth_code = $_GET["code"]; function getContent($url) { $curl = curl_init(); //The URL to fetch. curl_setopt($curl,CURLOPT_URL,$url); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //The number of seconds to wait while trying to connect.You can use 0 to wait indefinitely. curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5); //Set the contents of "User-Agent: " which will used in a HTTP request. $userAgent = $_SERVER["HTTP_USER_AGENT"]; curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //Set this TRUE To follow any "Location: " header that the server sends as part of the HTTP header. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect. curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //The maximum number of seconds to allow cURL functions to execute. curl_setopt($curl, CURLOPT_TIMEOUT, 10); //Set this false to stop cURL from verifying the peer's certificate. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $contents = curl_exec($curl); curl_close($curl); return $contents; } $fields=array( 'code'=> urlencode($auth_code), 'client_id'=> urlencode($client_id), 'client_secret'=> urlencode($client_secret), 'redirect_uri'=> urlencode($redirect_uri), 'grant_type'=> urlencode('authorization_code') ); $post = ''; foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; } $post = rtrim($post,'&'); $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token'); curl_setopt($curl,CURLOPT_POST,5); curl_setopt($curl,CURLOPT_POSTFIELDS,$post); curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0); $result = curl_exec($curl); curl_close($curl); $response = json_decode($result); $accesstoken = $response->access_token; $url = 'https://www.google.com/m8/feeds/contacts/default/full?&alt=json&max-results='.$max_results.'&oauth_token='.$accesstoken; $xmlresponse = getContent($url); if((strlen(stristr($xmlresponse,'Authorization required'))>0) && (strlen(stristr($xmlresponse,'Error '))>0)) { echo "There is some error.Try reloading the page."; exit(); } /*Custom Coding*/ if(count($xmlresponse)>0) { $decoded_data=json_decode($xmlresponse); echo '<table cellpadding="0" cellspacing="0">'; echo '<tr> <th style="padding: 3px;border-top: 1px solid #000;border-left: 1px solid #000;border-bottom: 1px solid #000;font-family: arial;font-size: 13px;">Email Address</th> </tr>'; foreach($decoded_data->feed->entry as $k=>$v) { $email_address=isset($v->{'gd$email'}[0]->address)?$v->{'gd$email'}[0]->address:''; $UserName=isset($v->title->{'$t'})?ucwords(strtolower($v->title->{'$t'})):''; $ContactNo=isset($v->{'gd$phoneNumber'}[0]->{'$t'})?$v->{'gd$phoneNumber'}[0]->{'$t'}:''; echo '<tr> <td style="padding: 3px;border-left: 1px solid #000;border-bottom: 1px solid #000;font-family: arial;font-size: 13px;">'.$email_address.'</td> </tr>'; } echo '</table>'; } } else { ?> <a href="https://accounts.google.com/o/oauth2/auth?client_id=<?php echo $client_id;?>&redirect_uri=<?php echo $redirect_uri;?>&scope=https://www.google.com/m8/feeds/&response_type=code">Import Gmail Contact</a> <?php } ?>
OUTPUT : You Will get all mail like this.
Share