Search code examples
phpxmlxpathcontactsgoogle-contacts-api

Google Contacts Full Name


I've searched pretty extensively, forgive me if this happens to be a repeat question.

I'm trying to retrieve first and last names with the Google Contacts api. Gathering emails was easy enough with:

$result = $xml->xpath('//gd:email');
$count = 0;
foreach ($result as $title) {
    $count++;
    $email = $title->attributes()->address;
}

I'm having absolutely no luck figuring out how to use

//gd:fullName

All help is appreciated.


Solution

  • The full name is not stored in an attribute, but as text value – that makes things even simpler for parsing.

    $names = $xml->xpath('//gd:fullName');
    foreach ($names as $name) {
        echo $name;
    }
    

    Whenever a SimpleXML node is casted to string, it returns its text value (here, the full name).