Skip to content

Instantly share code, notes, and snippets.

@rwaddin
Created June 17, 2020 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaddin/eb1a5d77dd83868b457fcb9f270759ee to your computer and use it in GitHub Desktop.
Save rwaddin/eb1a5d77dd83868b457fcb9f270759ee to your computer and use it in GitHub Desktop.
iam use codeigniter depend composer google api client
<?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