<?php

// This page handles the redirect from the authorisation page. It will authenticate your app and
// retrieve the refresh token which is used for long term access to Google Contacts. You should
// add this refresh token to the 'config.json' file.
if (!isset($_GET['code'])) {
    die('No code URL paramete present.');
}
$code = $_GET['code'];
require_once '../../../vendor/autoload.php';
use rapidweb\googlecontacts\helpers\GoogleHelper;
$client = GoogleHelper::getClient();
GoogleHelper::authenticate($client, $code);
$accessToken = GoogleHelper::getAccessToken($client);
if (!isset($accessToken->refresh_token)) {
    echo 'Google did not respond with a refresh token. You can still use the Google Contacts API, but you may to re-authorise your application in the future. ';
    echo 'Access token response:';
    var_dump($accessToken);
} else {
    echo 'Refresh token is: ' . $accessToken->refresh_token . ' - Please add this to the config file.';
}
<?php

// After filling in the clientID, clientSecret and redirectUri (within 'config.json'), you should visit this page
// to get the authorisation URL.
// Note that the redirectUri value should point towards a hosted version of 'redirect_handler.php'.
require_once '../vendor/autoload.php';
require_once '../lib/php-google-contacts-v3-api/helpers/GoogleHelper.php';
use rapidweb\googlecontacts\helpers\GoogleHelper;
$client = GoogleHelper::getClient();
$authUrl = GoogleHelper::getAuthUrl($client);
echo 'Go to the following URL to authorise your application for Google Contacts: ' . $authUrl;
echo "\r\n";
Exemplo n.º 3
0
 public static function create($name, $phoneNumber, $emailAddress)
 {
     $doc = new \DOMDocument();
     $doc->formatOutput = true;
     $entry = $doc->createElement('atom:entry');
     $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:atom', 'http://www.w3.org/2005/Atom');
     $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gd', 'http://schemas.google.com/g/2005');
     $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gd', 'http://schemas.google.com/g/2005');
     $doc->appendChild($entry);
     $title = $doc->createElement('title', $name);
     $entry->appendChild($title);
     $email = $doc->createElement('gd:email');
     $email->setAttribute('rel', 'http://schemas.google.com/g/2005#work');
     $email->setAttribute('address', $emailAddress);
     $entry->appendChild($email);
     $contact = $doc->createElement('gd:phoneNumber', $phoneNumber);
     $contact->setAttribute('rel', 'http://schemas.google.com/g/2005#work');
     $entry->appendChild($contact);
     $xmlToSend = $doc->saveXML();
     $client = GoogleHelper::getClient();
     $req = new \Google_Http_Request('https://www.google.com/m8/feeds/contacts/default/full');
     $req->setRequestHeaders(array('content-type' => 'application/atom+xml; charset=UTF-8; type=feed'));
     $req->setRequestMethod('POST');
     $req->setPostBody($xmlToSend);
     $val = $client->getAuth()->authenticatedRequest($req);
     $response = $val->getResponseBody();
     $xmlContact = simplexml_load_string($response);
     $xmlContact->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
     $xmlContactsEntry = $xmlContact;
     $contactDetails = array();
     $contactDetails['id'] = (string) $xmlContactsEntry->id;
     $contactDetails['name'] = (string) $xmlContactsEntry->title;
     foreach ($xmlContactsEntry->children() as $key => $value) {
         $attributes = $value->attributes();
         if ($key == 'link') {
             if ($attributes['rel'] == 'edit') {
                 $contactDetails['editURL'] = (string) $attributes['href'];
             } elseif ($attributes['rel'] == 'self') {
                 $contactDetails['selfURL'] = (string) $attributes['href'];
             }
         }
     }
     $contactGDNodes = $xmlContactsEntry->children('http://schemas.google.com/g/2005');
     foreach ($contactGDNodes as $key => $value) {
         $attributes = $value->attributes();
         if ($key == 'email') {
             $contactDetails[$key] = (string) $attributes['address'];
         } elseif ($key == 'organization') {
             $contactDetails[$key] = 'hello';
             //$contactDetails[$key] = (string) $value->children()->current() ?: 'hello';
         } else {
             $contactDetails[$key] = (string) $value;
         }
     }
     return new Contact($contactDetails);
 }
 public static function delete(Contact $toDelete)
 {
     $client = GoogleHelper::getClient();
     $req = new \Google_Http_Request($toDelete->editURL);
     $req->setRequestHeaders(array('content-type' => 'application/atom+xml; charset=UTF-8; type=feed'));
     $req->setRequestMethod('DELETE');
     $client->getAuth()->authenticatedRequest($req);
 }