Fetch gmail contacts along with Firstname, lastname, Phonenumber and email

1,606 views
Skip to first unread message

Rajeash

unread,
Feb 13, 2012, 4:46:32 AM2/13/12
to Google Contacts, Shared Contacts and User Profiles APIs
Hi,

I need to fetch gmail contacts using Oauth2.0 using PHP

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

foreach ($result as $title) {
echo $title->attributes()->address;
}


in the above code i am retrieving all email of the contacts.
Can anyone tell me that how i need to pick all infornation of my
contact like like
Firstname
Lastname
Phonenumber,
Email.

As of now it is displaying only email when i am using above code.
I need a same which should retrieve Email, firstname, lastname and
phone number.


Gavin Johnson

unread,
Feb 18, 2012, 10:50:38 AM2/18/12
to Google Contacts, Shared Contacts and User Profiles APIs
Hi Rajeash

I think this might be about namespaces and SimpleXML. Does this help?

foreach ($result as $title) {
$ns_gd = $title->children('http://schemas.google.com/g/2005');
echo $ns_gd->attributes()->address;
}

Gavin

*Rajeash*

unread,
Feb 29, 2012, 6:30:31 AM2/29/12
to google-co...@googlegroups.com
Hi Gavin,

Still i am not getting the result.

I am using the below code to fetch the contacts of Gmail, where it returns only Email of the user

$result = $xml->xpath('//gd:email');

I want to fetch Email, FirstName, LastName, PhoneNumber,OrganizationName, PostalAddress all simultanously.
May i know the Exactly how to fetch all the above information of the my all contacts.




--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
google-co...@googlegroups.com
To unsubscribe from this group, send email to
google-contacts...@googlegroups.com
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html



--
Rajeash
9962566123

Rajeash

unread,
Mar 7, 2012, 1:27:32 AM3/7/12
to Google Contacts, Shared Contacts and User Profiles APIs
Hi Gavin,

Still i am not getting the result.

I am using the below code to fetch the contacts of Gmail, where it
returns only Email of the user

$result = $xml->xpath('//gd:email');

I want to fetch Email, FirstName, LastName,
PhoneNumber,OrganizationName, PostalAddress all simultanously.
May i know the Exactly how to fetch all the above information of the
my all contacts.


On Feb 18, 8:50 pm, Gavin Johnson <snow...@gmail.com> wrote:
> Hi Rajeash
>
> I think this might be about namespaces and SimpleXML. Does this help?
>
> foreach ($result as $title) {
>   $ns_gd = $title->children('http://schemas.google.com/g/2005');
>   echo  $ns_gd->attributes()->address;
>
> }
>
> Gavin
>
> On Feb 13, 10:46 am, Rajeash <yraje...@gmail.com> wrote:
>
> > Hi,
>
> > I need to fetchgmailcontactsusing Oauth2.0 using PHP

B8X

unread,
Mar 8, 2012, 9:34:14 AM3/8/12
to Google Contacts, Shared Contacts and User Profiles APIs
Tell me please what do you use for the variable xmlresponse? I am
using. such a code to get contact information:
require_once ('google-api-php-client/src/apiClient.php');




$ Client = new apiClient ();
$ Client-> setApplicationName ('Google Contacts PHP Sample');
$ Client-> setScopes ("http://www.google.com/m8/feeds/");
/ / Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html
/ / Visit https://code.google.com/apis/console?api=contacts to
generate your
/ / Oauth2_client_id, oauth2_client_secret, and register your
oauth2_redirect_uri.
 $ Client-> setClientId ('somecode');
 $ Client-> setClientSecret ('somecode');
 $ Client-> setRedirectUri ('somecode');
 / / $ Client-> setDeveloperKey ('insert_your_developer_key');




if (isset ($ _GET ['code'])) {
  $ Client-> authenticate ();
  $ _SESSION ['Token'] = $ client-> getAccessToken ();
  $ Redirect = 'http://'. $ _SERVER ['HTTP_HOST']. $ _SERVER
['PHP_SELF'];
  header ('Location:'. filter_var ($ redirect, FILTER_SANITIZE_URL));
}




if (isset ($ _SESSION ['token'])) {
 $ Client-> setAccessToken ($ _SESSION ['token']);
}




if (isset ($ _REQUEST ['logout'])) {
  unset ($ _SESSION ['token']);
  $ Client-> revokeToken ();
}




if ($ client-> getAccessToken ()) {
  $ Req = new apiHttpRequest ("https://www.google.com/m8/feeds/
contacts/default/full") ;/ / create requests string contacts /
default / full
    / / $ Req = new apiHttpRequest ("http://schemas.google.com/
contacts/2008/rel # photo") ;/ / create requests string
  $ Val = $ client-> getIo () -> authenticatedRequest ($ req);
    print "<pre>";
    print_r ($ val);
    print "</ pre>";
  / / The contacts api only returns XML responses.
  $ Response = json_encode (simplexml_load_string ($ val->
getResponseBody ()));
  print "Debugging code for Google API <pre>". print_r (json_decode ($
response, true), true). "</ Pre>";




  / / The access token may have been updated lazily.
  $ _SESSION ['Token'] = $ client-> getAccessToken ();
} Else {
  $ Auth = $ client-> createAuthUrl ();
}




if (isset ($ auth)) {
    print "<a class=login href='$auth'> Connect Me! </ a>";
  } Else {
    print "<a class=logout href='?logout'> Logout </ a>";
}
___________
How to follow the code of your part of the code to display a list of
contacts, emails and not merely the title and content???

Baptiste Lafontaine

unread,
Mar 27, 2012, 12:07:21 PM3/27/12
to google-co...@googlegroups.com
You should use "$val->getResponseBody()" which contains the raw XML data.
Reply all
Reply to author
Forward
0 new messages