Created
June 17, 2020 08:57
-
-
Save rwaddin/eb1a5d77dd83868b457fcb9f270759ee to your computer and use it in GitHub Desktop.
iam use codeigniter depend composer google api client
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
<?php | |
public function index() | |
{ | |
$google_redirect_uri = 'your url callback'; | |
//setup new google client | |
$client = new Google_Client(); | |
$client -> setApplicationName('My application name'); | |
$client->setAuthConfigFile("./client_secret.json"); | |
$client -> setRedirectUri($google_redirect_uri); | |
$client -> setAccessType('online'); | |
$client -> setScopes('https://www.google.com/m8/feeds'); | |
if (isset($_GET['code'])) { | |
$client->authenticate($_GET['code']); | |
$_SESSION["token"] = $client->getAccessToken(); | |
} | |
if (!isset($_SESSION['token'])) { | |
$googleImportUrl = $client -> createAuthUrl(); | |
echo "<a href='{$googleImportUrl}'> Import google contacts </a>"; | |
}else{ | |
$client->setAccessToken($_SESSION['token']); | |
// $token = json_decode($_SESSION['token']); | |
// $token->access_token(); | |
$token = $client->getAccessToken(); | |
$token = $token["access_token"]; | |
// print_r($token); | |
$x = "https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=10&access_token={$token}"; | |
$ch = curl_init($x); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
$rs = curl_exec($ch); | |
// echo "<pre>"; | |
// print_r (json_decode($rs,true)); | |
// echo "</pre>"; | |
$arr = array(); | |
$contacts = json_decode($rs,true); | |
foreach ($contacts["feed"]["entry"] as $row) { | |
$arr[] = [ | |
"name" => isset($row["title"]['$t']) ? $row["title"]['$t'] :"kosong", | |
"phone" => isset($row['gd$phoneNumber'][0]['$t']) ? $row['gd$phoneNumber'][0]['$t']:"", | |
]; | |
} | |
echo "<pre>"; | |
// print_r ($contacts); | |
print_r ($arr); | |
echo "</pre>"; | |
session_destroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment