Stuart McLaren
Stuart McLaren

Reputation: 119

Get Google Contact's nickname in PHP

I have managed to successfully get a json feed of my Google contacts and can retrieve the Name and address from this. I also need to retrieve the contact's nickname, and I cannot figure out how to do this? I've used quite a few examples I have found to get to the point I am at, to be honest there is quite a lot going on I don't really understand! (mainly to do with cURL) but hopefully this code snippet will help

$temp = json_decode($xmlresponse,true);
foreach($temp['feed']['entry'] as $cnt) {       
   $address=$cnt['gd$postalAddress'][0]['$t'];
   $title  =$cnt['title']['$t'];
}

From what I have read, I was hoping that $cnt['gContact$nickname'][0]['$t']; would return the nickname, but it doesn't. I have tried dumping the $xmlresponse, $temp and $cnt variables, but can find no reference to nickname in any of them.

Can anyone explain how to find the nickname?

Thanks.

19/11/2015

Thanks for your reply, Rael, which is useful to me, but I still don't fully understand. I have tried requesting my Google Contacts feed without the "&alt=json" paramater, but when I do a var_dump of the returned data, I can still see nothing about nickname?

I tried adding the following code, but am still not getting anything returned.

$xml->registerXPathNamespace('gContact',     'http://schemas.google.com/contact/2008');
$result = $xml->xpath('//gContact:nickname');
foreach ($result as $cnt){
    $nickname=$cnt['gContact$nickname']['$t'];
    echo $nickname;
}

Upvotes: 0

Views: 100

Answers (1)

Rael Gugelmin Cunha
Rael Gugelmin Cunha

Reputation: 3532

You need to use http://schemas.google.com/contact/2008 as XML namespace to get contacts-specific elements like nickname.

Nickname is returned like this:

<gContact:nickname>Dragon</gContact:nickname>

Read more in the Additional Extensions of the Contact kind.

Upvotes: 1

Related Questions