Last active
September 11, 2020 06:54
-
-
Save rwaddin/df78cb6d5eb8908c0acf5e6b1d5d185e to your computer and use it in GitHub Desktop.
for update google contact API, with ci 3.1.1 but you can use anywhere :D
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 | |
if (! function_exists('contact_check')) | |
{ | |
function contact_check() | |
{ | |
$CI =& get_instance(); | |
$client = new Google_Client(); | |
$client->setApplicationName('Whatsapp'); | |
$client->setScopes([ | |
"https://www.google.com/m8/feeds", | |
"https://www.googleapis.com/auth/contacts" | |
]); | |
$client->setAuthConfig($CI->config->item("path_secret")); | |
$client->setAccessType('offline'); | |
$client->setPrompt('select_account consent'); | |
$tokenPath = $CI->config->item("path_token"); | |
if (file_exists($tokenPath)) { | |
$accessToken = json_decode(file_get_contents($tokenPath), true); | |
$client->setAccessToken($accessToken); | |
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); | |
/*log_message('error','refresh token : '.print_r($client->getRefreshToken(), true)); | |
log_message('error','acces token old : '.print_r($accessToken, true)); | |
log_message('error','acces token new: '.print_r($client->getAccessToken(), true)); | |
log_message('error','A'.print_r($client,true));*/ | |
return $client->getAccessToken(); | |
}else{ | |
# token not avilable | |
return false; | |
} | |
} | |
} |
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 | |
/** | |
* Get API Google contact by phone number | |
* di get request queryna bisa bebas, cuma disini hanya digunakan untuk get phone | |
*/ | |
if (! function_exists('contact_getPhone')) | |
{ | |
function contact_getPhone($phone, $token) | |
{ | |
if ($phone) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/m8/feeds/contacts/default/full?q={$phone}&alt=json&access_token={$token}"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
$respons = json_decode($result,true); | |
print_r($respons); | |
if (isset($respons["feed"]["entry"])) { | |
$data = array(); | |
$data["author"]["email"] = $respons["feed"]["author"][0]["email"]['$t']; | |
$data["author"]["name"] = $respons["feed"]["author"][0]["name"]['$t']; | |
foreach ($respons["feed"]["entry"] as $row) { | |
# get contact id | |
$exp = explode("/", $row["id"]['$t']); | |
$data["id_raw"] = end($exp); # if yg sudah explode | |
$data["id_ori"] = $row["id"]['$t']; | |
$data["name"] = $row["title"]['$t']; | |
$data["phone"] = $row['gd$phoneNumber'][0]['$t']; | |
$data["phonefull"] = ltrim($row['gd$phoneNumber'][0]['uri'],"tel:"); | |
$data["note"] = isset($row["content"]['$t']) ? $row["content"]['$t'] : ""; | |
$data["email"] = isset($row['gd$email'][0]['address']) ? $row['gd$email'][0]['address'] : false; # isinya array kita hanya ambil index 0 | |
} | |
return [ | |
"stts" => true, | |
"count" => count($respons["feed"]["entry"]), | |
"data" => $data | |
]; | |
} | |
} | |
return ["stts"=>false]; | |
} | |
} |
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 | |
/** | |
* init 2020-09-10 | |
* get contact by phone (use get param) | |
* proses update | |
*/ | |
if (! function_exists('contact_setUpdate')) | |
{ | |
function contact_setUpdate() | |
{ | |
$token = contact_check(); | |
$token_access = $token["access_token"]; | |
$phone = "85747277466"; | |
$client = contact_getPhone($phone, $token_access); | |
print_r($client); | |
$id_contact = $client["data"]["id_raw"]; | |
$update = '<?xml version="1.0" encoding="UTF-8"?> | |
<entry | |
xmlns:gd = "http://www.w3.org/TR/html4/" | |
xmlns:gContact = "http://www.w3.org/TR/html4/"> | |
<category scheme="https://schemas.google.com/g/2005#kind" term="https://schemas.google.com/contact/2008#contact"/> | |
<gd:name> | |
<gd:fullName>nama baru (fullname)</gd:fullName> | |
</gd:name> | |
<gd:phoneNumber label="Mobile" primary="true">+6598765432</gd:phoneNumber> | |
<gd:phoneNumber label="Father">+6598732465</gd:phoneNumber> | |
<gd:phoneNumber label="Friend">+6589898989</gd:phoneNumber> | |
<gContact:groupMembershipInfo deleted="false" href="https://www.google.com/m8/feeds/groups/rw.chat01@gmail.com/base/6"/> | |
</entry>'; | |
#cURL | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/m8/feeds/contacts/default/full/{$id_contact}"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $update); | |
$headers = array(); | |
$headers[] = 'Authorization: Bearer '.$token_access; | |
$headers[] = 'Content-Type: application/atom+xml; charset=UTF-8; type=feed'; | |
$headers[] = 'Gdata-Version: 3.0'; | |
$headers[] = "If-Match: *"; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
} | |
curl_close($ch); | |
print_r($result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment