-
-
Save anonymous/18cc65232afebf280645 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$authUrl = 'https://accounts.google.com/o/oauth2/auth'; | |
$authData = array( | |
'client_id' => '587426018136-qbqv5d32bcu28kd1a1mrjoa6fu2d4gfm.apps.googleusercontent.com', | |
'redirect_uri' => 'http://valerij.pp.ua/gmail-contacts-import-demo/', | |
'scope' => 'https://www.google.com/m8/feeds/', | |
'response_type' => 'code' | |
); | |
//ссылка для запроса на авторизацию | |
$gmailAuthURL = $authUrl.'?'.http_build_query($gmailAuthData); | |
//------------------------------------------------------ | |
$authCode = isset($_GET["code"])?$_GET["code"]:false; | |
$errorCode = isset($_GET["error"])?$_GET["error"]:false; | |
if ($errorCode == 'access_denied'){ | |
die("Доступ запрещен"); | |
} | |
session_start(); | |
//Проверяем есть ли уже в сессии полученный ранее токен | |
//Если он уже есть и срок действия не истек - используем его | |
$getNewToken = true; | |
if (isset($_SESSION['access_token']) && $_SESSION['access_token_expires']){ | |
if($_SESSION['access_token_expires'] > time()){ | |
$getNewToken = false; | |
$accessToken = $_SESSION['access_token']; | |
} | |
} | |
//если пришел код авторизации и токен ещё не получен | |
if ($authCode && $getNewToken){ | |
$oauthURL = 'https://accounts.google.com/o/oauth2/token'; | |
$oauthData = array( | |
'code' => $authCode, | |
'client_id' => '587426018136-qbdv5d32bau28kd1afakeoa6fu2d4gfm.apps.googleusercontent.com', | |
'client_secret' => 'fakeNcTKwY_-Qi-t0LC2erXW', | |
'redirect_uri' => 'http://valerij.pp.ua/gmail-contacts-import-demo/', | |
'grant_type' => 'authorization_code' | |
); | |
$oauthResponse = curl_post_get_result($oauthURL, $oauthData); | |
if(isset($oauthResponse->error)){ | |
die('Ошибка авторизации'); | |
} | |
$accessToken = $oauthResponse->access_token; | |
$accessTokenExp = $oauthResponse->expires_in; | |
//сохраняем в сессию полученный токен | |
$_SESSION['access_token'] = $accessToken; | |
$_SESSION['access_token_expires'] = time()+$accessTokenExp; | |
} | |
else{ | |
die('Ошибка авторизации'); | |
} | |
$queryURL = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=500&oauth_token='.$accessToken; | |
$xmlresponse = curl_file_get_contents($queryURL); | |
//В случае получения ошибки авторизации от Google. | |
if((strlen(stristr($xmlresponse,'Authorization required'))>0) || (strlen(stristr($xmlresponse,'Error '))>0)) | |
{ | |
die('Ошибка авторизации'); | |
} | |
$xml = simplexml_load_string($xmlresponse); | |
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005'); | |
$result = $xml->xpath('//gd:email'); | |
$resultData = array(); | |
if ($result){ | |
foreach ($result as $emailData) { | |
$email = $emailData->attributes()->address; | |
$userEmail = $email->__toString(); | |
$entry = $emailData->xpath(".."); | |
$name = $entry[0]->title; | |
$userName = $name->__toString(); | |
$resultData[] = array( | |
'email' => $userEmail, | |
'name' => $userName | |
); | |
} | |
print(resultData); exit; | |
} | |
else{ | |
die('Список контактов пустой'); | |
} | |
//------------------------------------------------------ | |
function curl_post_get_result($url, $postData){ | |
$post = http_build_query($postData); | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_POST, 5); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); | |
$result = curl_exec($curl); | |
curl_close($curl); | |
return $result; | |
} | |
//------------------------------------------------------ | |
function curl_file_get_contents($url){ | |
$curl = curl_init(); | |
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); | |
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); | |
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE) | |
curl_setopt($curl, CURLOPT_TIMEOUT, 10); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); | |
$contents = curl_exec($curl); | |
curl_close($curl); | |
return $contents; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment