webjem
webjem

Reputation: 195

Google Contacts API name and email

This is my first post on this site, so forgive me if I butcher it, but I'll try to be a clear and straight forwards as possible.

I'm trying to use the Google Contacts API to import the name and email address from an authenticated users gmail account. I am getting the email address fine using the generic code supplied by Google itself. I've tried to modify it to grab the contact names as well to no avail. Any help would be greatly appreciated. Below is the code I am currently using.

$xml =  new SimpleXMLElement($val->getResponseBody());

$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');

$name_result = $xml->xpath('//title');

foreach ($result as $title) {
    echo "<div>".$title->attributes()->address."</div>";
}

foreach ($name_result as $name) {
        echo "<div class='contact_alt'>".$name."</div>";
}

Upvotes: 4

Views: 3966

Answers (2)

Chandrew
Chandrew

Reputation: 17277

An alternative solution I use is asking the Google API to reply with JSON, rather than XML. Parsing the XML with the gd: namespaces gets tricky. The JSON comes back with contact names and emails and it becomes a matter of using json_decode().

$url = 'https://www.google.com/m8/feeds/contacts/%s/full'
$url .= '&access_token=%s'
$url .= &alt=json;

Just add the &alt=json to the request URL.

Upvotes: 1

webjem
webjem

Reputation: 195

OK, so I managed to find the answer elsewhere on this site. Didn't mean to post a question with an answer that already existed here. I really did try to look around first so I didn't do that.

PHP GMAIL Contacts XML Parsing with DOMDocument and cURL

Upvotes: 2

Related Questions