Google Contacts API Name and Address

This is my first post on this site, so forgive me if I destroy it, but I will try to be as clear and direct forward as possible.

I am trying to use the Google Contacts API to import a name and email address from an authenticated gmail account. I get the email address in order using the common code provided by Google itself. I tried changing it to get contact names, but to no avail. Any help would be greatly appreciated. Below is the code I'm 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>"; } 
+4
source share
2 answers

OK, so I managed to find the answer elsewhere on this site. This does not mean that you should ask a question with an answer that already exists here. I really tried to look around first, so I did not.

PHP GMAIL Contact Us XML Parsing with DOMDocument and cURL

+2
source

An alternative solution that I use is to ask the Google API to respond with JSON, not XML. XML parsing with gd: namespaces is getting complicated. JSON comes back with contact names and email, and this 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 &alt=json to the request url.

+1
source

All Articles