1. Create an application
Https://code.google.com/apis/console/
Set the corresponding application name to the URL address
Under API access
Oauth2_client_id, oauth2_client_secret, oauth2_redirect_uri
2. Download the Google client tool for PHP
Https://developers.google.com/google-apps/tasks/downloads? Hl = ZH-CN
3. Set config. php
Configuration
'Oss _ client_id '=> '6644545content. com ',
'Oss _ client_secret '=> 'zjcfno45452sb6hzgkcp3 ',
'Oss _ redirect_uri '=> 'HTTP: // e454545com/login/Gmail /',
4. The program code is as follows:
In the example provided by Google, only the User Name of the mailbox can be obtained, but the email address is not obtained. Therefore, the email address must be obtained twice and then merged.
require_once ('BaseAction.php');require_once ("../lib/google_lib/apiClient.php");$client = new apiClient();$client->setApplicationName('Engir');$client->setScopes("http://www.google.com/m8/feeds/");// Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html// Visit https://code.google.com/apis/console?api=contacts to generate your// oauth2_client_id, oauth2_client_secret, and register your oauth2_redirect_uri.// $client->setClientId('insert_your_oauth2_client_id');// $client->setClientSecret('insert_your_oauth2_client_secret');// $client->setRedirectUri('insert_your_redirect_uri');// $client->setDeveloperKey('insert_your_developer_key');if (isset($_GET['code'])) { $client->authenticate(); $_SESSION['token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));}if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']);}if (isset($_REQUEST['logout'])) { unset($_SESSION['token']); $client->revokeToken(); header("location:".Config::ROOT_URL."/invites/gmail/");}if ($client->getAccessToken()) { $req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full?max-results=100"); $val = $client->getIo()->authenticatedRequest($req); // The contacts api only returns XML responses. $response = json_encode(simplexml_load_string($val->getResponseBody())); $nameArray = array(); $responseArray = json_decode($response, true); if (isset($responseArray["entry"])){ $entry = $responseArray["entry"]; foreach ($entry as $item){ if(!is_array($item["title"])){ array_push($nameArray,$item["title"]); }else { array_push($nameArray,"##"); } } } // The access token may have been updated lazily. $_SESSION['token'] = $client->getAccessToken(); //print "<pre>" . print_r(json_decode($response, true), true) . "</pre>"; //reading xml using SimpleXML $emailArray = array(); $xml= new SimpleXMLElement($val->getResponseBody()); $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005'); $result = $xml->xpath('//gd:email'); foreach ($result as $title) { $email = $title->attributes()->address; if (!empty($email)){ array_push($emailArray,$email.""); } } $userArray = array(); for ($i=0;$i<count($emailArray);$i++){ $user = array(); $name = $nameArray[$i]; $email = $emailArray[$i]; if ($name == "##"){ $name = strstr($email, '@', true); } $user["name"] = $name; $user["email"] = $email; array_push($userArray,$user); }
The URL used to verify the request is generated in this way.
$client = new apiClient();$client->setApplicationName('Engir');$client->setScopes("http://www.google.com/m8/feeds/");$auth = $client->createAuthUrl();
Reference URL:
1. https://developers.google.com/google-apps/contacts/v3/
2. http://lookmywebpage.com/api/google/import-gmail-or-google-contacts-using-php-and-oauth-2-0/