xml - Google Contacts API get phone number (PHP) -
i'm using google contacts api , i'm able extract names , email addresses i'd profile pictures , phone numbers.
i'm using php , here's code while authenticating:
//if authenticated successfully... $req = new google_httprequest("https://www.google.com/m8/feeds/contacts/default/full"); $val = $client->getio()->authenticatedrequest($req); $doc = new domdocument; $doc->recover = true; $doc->loadxml($val->getresponsebody()); $xpath = new domxpath($doc); $xpath->registernamespace('gd', 'http://schemas.google.com/g/2005'); $emails = $xpath->query('//gd:email'); foreach ( $emails $email ){ echo $email->getattribute('address'); //successfully gets person's email address echo $email->parentnode->getelementsbytagname('title')->item(0)->textcontent; //successfully gets person's name }
phone number
this part getting phone number doesn't work.
$phone = $xpath->query('//gd:phonenumber'); foreach ( $phone $row ){ print_r($row); // part doesnt work }
profile picture
judging api link above, looks can grab profile picture url: https://www.google.com/m8/feeds/contacts/default/full
i'm not sure how find within domxpath
$xpath
object generated.
thoughts?
the google contacts api uses atom feed. contacts provided entry
elements. makes more sense iterate them. have register prefix atom namespace well.
$document = new domdocument(); $document->loadxml($xml); $xpath = new domxpath($document); $xpath->registernamespace('atom', 'http://www.w3.org/2005/atom'); $xpath->registernamespace('gd', 'http://schemas.google.com/g/2005');
if use domxpath::evaluate() can use expressions return scalars. second argument context node expression.
foreach ($xpath->evaluate('/atom:feed/atom:entry') $entry) { $contact = [ 'name' => $xpath->evaluate('string(atom:title)', $entry), 'image' => $xpath->evaluate('string(atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href)', $entry), 'emails' => [], 'numbers' => [] ]; foreach ($xpath->evaluate('gd:email', $entry) $email) { $contact['emails'][] = $email->getattribute('address'); } foreach ($xpath->evaluate('gd:phonenumber', $entry) $number) { $contact['numbers'][] = trim($number->textcontent); } var_dump($contact); }
with first example response google contacts api documentation returns:
array(3) { ["name"]=> string(17) "fitzwilliam darcy" ["image"]=> string(64) "https://www.google.com/m8/feeds/photos/media/useremail/contactid" ["email"]=> string(0) "" ["numbers"]=> array(1) { [0]=> string(3) "456" } }
the example not include email
element, empty. contact can have multiple email addresses and/or phone numbers or none @ all. rel
attribute provided classify them home, work, ...
fetching image:
the image provided atom link
element specific rel
attribute.
atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]
this return link
node, possible fetch href attribute directly:
atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href
cast attribute node list string:
string(atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href)
Comments
Post a Comment