Skip to content

Instantly share code, notes, and snippets.

@rwaddin
Created June 29, 2020 01:53
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/a0d9deb336bf81bc3b5eff0f31fa3d22 to your computer and use it in GitHub Desktop.
Save rwaddin/a0d9deb336bf81bc3b5eff0f31fa3d22 to your computer and use it in GitHub Desktop.
Membuat kontak pada google menggunakan API offline access
<?php
if (! function_exists('contact_create'))
{
function contact_create($params = false)
{
if (isset($params["phone"])) {
$CI =& get_instance();
$nama = $params["nama"];
$phone = formatPhone($params["phone"]);
$token = contact_check();
# email yang digunakan untuk menyimpan contact & harus login app dahulu
$email_contact = $CI->config->item("contact_mail");
$note = isset($params["note"]) ? $params["note"] : "Auto create whatsapp sales";
$email = isset($params["email"]) ? "<gd:email rel='http://schemas.google.com/g/2005#home' address='{$params["email"]}'/>" : "";
$contact = "
<atom:entry xmlns:atom='http://www.w3.org/2005/Atom'
xmlns:gd='http://schemas.google.com/g/2005'
xmlns:gContact='http://schemas.google.com/contact/2008'>
<atom:category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/contact/2008#contact'/>
<gd:name>
<gd:fullName>{$nama}</gd:fullName>
</gd:name>
{$email}
<atom:content type='text'>{$note}</atom:content>
<gd:phoneNumber rel='http://schemas.google.com/g/2005#home'>
{$phone}
</gd:phoneNumber>
<gContact:groupMembershipInfo deleted='false'
href='http://www.google.com/m8/feeds/groups/{$email_contact}/base/6'/>
</atom:entry>
";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/m8/feeds/contacts/default/full");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $contact);
$headers = array();
$headers[] = 'Content-Type: application/atom+xml; charset=UTF-8; type=feed';
$headers[] = 'Gdata-Version: 3.0';
$headers[] = "Authorization: Bearer {$token["access_token"]}";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
log_message('error',"CREATE CONTACT GOOGLE ".print_r(["params"=>$params, "respons"=>$result],true));
if (is_object(json_decode($result))) {
# gagal (biasanya responya json)
$contact = "<b>Gagal create: </b> \n ";
$contact .= base64_encode($result);
}else{
# sukses (pasti xml)
$rs = new SimpleXMLElement($result);
// $contact = "id " .$rs->id ."\n";
$contact = "<b>Nama :</b> " .$rs->title ."\n";
$contact .= "<b>Phone :</b> " .$phone ."\n";
$contact .= isset($params["email"]) ? "<b>Email :</b> {$params["email"]}\n": "";
$contact .= "<b>Note :</b> \n" .$rs->content;
}
$report = "<b>[ Report Whatsapp Billing ]</b> \n";
$report .= "respon contact create \n";
$report .= $contact;
$report .= "\n========[end]=======";
// sendTelegram("-189542399", $report);
sendTelegram(0, $report);
return $contact;
}
}
}
if (! function_exists('contact_check'))
{
function contact_check()
{
$CI =& get_instance();
$client = new Google_Client();
$client->setApplicationName('Whatsapp Sales');
$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{
sendTelegram(0, "<b>[ Whatsapp sales ]<b> \nloc : contact_helper.php \ncase : token create contact API Goolge tidak ditemukan, mungkin admin belum melakukan login :)");
# token not avilable
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment