Contact API for PHP

1,695 views
Skip to first unread message

rchrd

unread,
Nov 6, 2008, 2:32:24 AM11/6/08
to Google Contacts API
help!

I was able to have a success login using ClientLogin but what will I
do next? I'm on https://www.google.com/accounts/ClientLogin How can i
use the Auth? And placed it in a variable and make a new request. I'm
using PHP.

Julian (Google)

unread,
Nov 12, 2008, 6:33:33 AM11/12/08
to Google Contacts API
Hi,

After the Authentication, you can use a Gdata_Query to get a
Gdata_Feed with Gdata_Entries from the API.

http://framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_Feed.html
http://framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_Entry.html

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Gdata_Query');

// Using Client Login
$client = Zend_Gdata_ClientLogin::getHttpClient("US...@domain.com",
"PASSWORD", "cp");
$gdata = new Zend_Gdata($client);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/
USER%40domain.com/full');
$query->setMaxResults(100);
$feed = $gdata->getFeed($query);

foreach ($feed as $entry) {
$XMLContent = $entry->getXML();
print_r( $XMLContent );
}

The variable $XMLContent, contains the XML information for each
contact, because there isn't a specific Gdata_Entry for Contacts, you
need to work with the XML directly.

What other operations do you need to perform?

Cheers,
Julian.

On Nov 6, 7:32 am, rchrd <rchr...@gmail.com> wrote:
> help!
>
> I was able to have a success login using ClientLogin but what will I
> do next? I'm onhttps://www.google.com/accounts/ClientLoginHow can i

Kris

unread,
Nov 13, 2008, 2:41:29 PM11/13/08
to Google Contacts API
Hello,
I am needing code for this.
I can get all the data for contact information, and even do some
basic work such as
$entry = $gdata->newEntry();
$entry->title = $gdata->newTitle('Test Email Address');

However, I am not sure how to add an email address for the contact.
And how would I create a contact group, and add contacts to that
contact group?
I was able to do it with the javascript api but I need a PHP backend
instead. (Or I need better documentation for the Javascript.. it shows
how to do basic API stuff, but I can't seem to figure out how to add a
contact to a group in Javascript.
Any help would be appreciated.
Thanks

On Nov 12, 6:33 am, "Julian (Google)" <j...@google.com> wrote:
> Hi,
>
> After the Authentication, you can use a Gdata_Query to get a
> Gdata_Feed with Gdata_Entries from the API.
>
> http://framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_Feed.htmlhttp://framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_Entry.html
>
> require_once 'Zend/Loader.php';
> Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
> Zend_Loader::loadClass('Zend_Gdata_Gapps');
> Zend_Loader::loadClass('Zend_Gdata_Query');
>
> // Using Client Login
> $client = Zend_Gdata_ClientLogin::getHttpClient("U...@domain.com",
> "PASSWORD", "cp");
> $gdata = new Zend_Gdata($client);
> $query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/
> USER%40domain.com/full');
> $query->setMaxResults(100);
> $feed = $gdata->getFeed($query);
>
> foreach ($feed as $entry) {
>   $XMLContent = $entry->getXML();
>   print_r( $XMLContent );
>
> }
>
> The variable $XMLContent, contains the XML information for each
> contact, because there isn't a specific Gdata_Entry for Contacts, you
> need to work with the XML directly.
>
> What other operations do you need to perform?
>
> Cheers,
> Julian.
>
> On Nov 6, 7:32 am, rchrd <rchr...@gmail.com> wrote:
>
> > help!
>
> > I was able to have a success login using ClientLogin but what will I
> > do next? I'm onhttps://www.google.com/accounts/ClientLoginHowcan i

Kris

unread,
Nov 14, 2008, 11:31:02 AM11/14/08
to Google Contacts API
Actually I was able to figure out the PHP code.
If anyone else is interested in how to add new contacts here is the
code I used

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Gdata_Query');


$email = "YOUR EMAIL";
$password = "YOUR PASSWORD"

$contactName = "YOUR CONTACT NAME";
$contactAddress = "YOUR CONTACT EMAIL ADDRESS";

$client = Zend_Gdata_ClientLogin::getHttpClient($email, $password,
"cp");
$gdata = new Zend_Gdata($client);

$extension = new Zend_Gdata_App_Extension_Element('email', null,
'http://schemas.google.com/g/2005');
$attributes['address'] = array('name'=>'address', 'value' =>
$contactAddress);
$attributes['rel'] = array('name'=>'rel', 'namespaceUri'=>null,
'value' => 'http://schemas.google.com/g/2005#other');
$attributes['primary'] = array('name'=>'primary', 'namespaceUri' =>
null, 'value' => 'true');

$extension->setExtensionAttributes($attributes);
$entry = $gdata->newEntry();
$entry->title = $gdata->newTitle($contactName);
$entry->setExtensionElements(array($extension));
$entryResult = $gdata->insertEntry($entry,"http://www.google.com/m8/
feeds/contacts/$email/full");


On Nov 13, 2:41 pm, Kris <nac.k...@gmail.com> wrote:
> Hello,
> I am needing code for this.
> I can get all the data for contact information, and even do some
> basic  work such as
> $entry = $gdata->newEntry();
> $entry->title = $gdata->newTitle('Test Email Address');
>
> However, I am not sure how to add an email address for the contact.
> And how would I create a contact group, and add contacts to that
> contact group?
> I was able to do it with the javascript api but I need a PHP backend
> instead. (Or I need better documentation for the Javascript.. it shows
> how to do basic API stuff, but I can't seem to figure out how to add a
> contact to a group in Javascript.
> Any help would be appreciated.
> Thanks
>
> On Nov 12, 6:33 am, "Julian (Google)" <j...@google.com> wrote:
>
> > Hi,
>
> > After the Authentication, you can use a Gdata_Query to get a
> > Gdata_Feed with Gdata_Entries from the API.
>
> >http://framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_Feed.html...

Julian (Google)

unread,
Nov 17, 2008, 7:03:44 AM11/17/08
to Google Contacts API
Hi Kris,

Thank you for sharing this example.

-julian

pierrick...@googlemail.com

unread,
Dec 13, 2008, 9:26:46 AM12/13/08
to Google Contacts API
Hello everybody,


I could find out the best way to parse through the resuling contact
feed.

Do you have any suggestions or tips how to do it?

The situation is, that I have the the whole Atom XLM Feed from ($feed-
>getXML())
and now I want to parse through the entries to fill my PHP-Array with
the infos like Email Name Street etc.

Couldn't find the right approach since simplexml can't parse it
right. :-(

PS: Is simplePie the solution?
Message has been deleted

Julian (Google)

unread,
Dec 15, 2008, 12:51:56 PM12/15/08
to Google Contacts API
Hi,

You can use SimpleXML using the appropriate XML namespaces, for
example:

$feed = $gdata->getFeed($query);

$xml = new SimpleXMLElement($feed->getXML());
$entries = $xml->children('http://www.w3.org/2005/Atom');

foreach ($entries->entry as $entry ) {
$defaults = $entry->children('http://schemas.google.com/g/2005');
echo 'title: '. $entry->title;
echo ' email: '.$defaults->email->attributes()->address;
echo ' address: '.$defaults->postalAddress;
echo "\n";
}

Cheers,
Julian.

On Dec 13, 2:26 pm, "pierrick.rave...@googlemail.com"

pierrick...@googlemail.com

unread,
Dec 16, 2008, 9:03:50 AM12/16/08
to Google Contacts API
Thank you Julian!

That worked like a charm... I did not know about the namespace thing.
Thanks again. ;-)

now I got another little problem with $defaults->email->attributes()-
>address;

when an entry does not have an emailadress this call ends up in the
following error message:

....: Node no longer exists

Ok that makes sense because it does not exist at all but how do I
check if this node exists?
if(!isset($defaults->email->attributes()->address)) does not fix it?

I think I overlook something but what?

Thank you in advance...

Julian (Google)

unread,
Dec 16, 2008, 11:42:41 AM12/16/08
to Google Contacts API
Hi,

I think you should look for the node first, before accessing the
attributes:
if ( isset($defaults->email)) {...

Cheers,
Julian

On Dec 16, 2:03 pm, "pierrick.rave...@googlemail.com"

pierrick...@googlemail.com

unread,
Dec 18, 2008, 6:09:19 AM12/18/08
to Google Contacts API
Yes that works - of course - hmm I was so shure that I tried that -
but still got the same error.

I guess I have to concentrate more!

Thanks again Julian.

rat86

unread,
Jan 8, 2009, 11:40:05 AM1/8/09
to Google Contacts API
i have tried this code, bit receive this error:

Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with
message 'Expected response code 200, got 403 <HTML> <HEAD>
<TITLE>Cannot request contacts belonging to another user</TITLE> </
HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Cannot request
contacts belonging to another user</H1> <H2>Error 403</H2> </BODY> </
HTML> ' in /home/rat86/public_html/Gmail/library/Zend/Gdata/App.php:
644 Stack trace: #0 /home/rat86/public_html/Gmail/library/Zend/
Gdata.php(206): Zend_Gdata_App->performHttpRequest('GET', 'http://
www.goog...', Array, NULL, NULL, NULL) #1 /home/rat86/public_html/
Gmail/library/Zend/Gdata/App.php(782): Zend_Gdata->performHttpRequest
('GET', 'http://www.goog...', Array) #2 /home/rat86/public_html/Gmail/
library/Zend/Gdata/App.php(686): Zend_Gdata_App->get('http://
www.goog...', Array) #3 /home/rat86/public_html/Gmail/library/Zend/
Gdata/App.php(194): Zend_Gdata_App->importUrl('http://www.goog...',
'Zend_Gdata_Feed') #4 /home/rat86/public_html/Gmail/library/Zend/
Gdata.php(153): Zend_Gdata_App->getFeed('http: in /home/rat86/
public_html/Gmail/library/Zend/Gdata/App.php on line 644


my code is:

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Gdata_Query');

// Using Client Login
$client = Zend_Gdata_ClientLogin::getHttpClient
("ra...@gmail.com","*********", "cp");
$gdata = new Zend_Gdata($client);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/
rat86%40gmail.com/full');
$query->setMaxResults(100);
$feed = $gdata->getFeed($query);

foreach ($feed as $entry) {
$XMLContent = $entry->getXML();
print_r( $XMLContent );
}


what is the error?
thanks








On 12 Nov 2008, 12:33, "Julian (Google)" <j...@google.com> wrote:
> Hi,
>
> After the Authentication, you can use a Gdata_Query to get a
> Gdata_Feed with Gdata_Entries from the API.
>
> http://framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_Feed.htmlhttp://framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_Entry.html
>
> require_once 'Zend/Loader.php';
> Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
> Zend_Loader::loadClass('Zend_Gdata_Gapps');
> Zend_Loader::loadClass('Zend_Gdata_Query');
>
> // Using Client Login
> $client = Zend_Gdata_ClientLogin::getHttpClient("U...@domain.com",
> "PASSWORD", "cp");
> $gdata = new Zend_Gdata($client);
> $query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/
> USER%40domain.com/full');
> $query->setMaxResults(100);
> $feed = $gdata->getFeed($query);
>
> foreach ($feed as $entry) {
>   $XMLContent = $entry->getXML();
>   print_r( $XMLContent );
>
> }
>
> The variable $XMLContent, contains the XML information for each
> contact, because there isn't a specific Gdata_Entry for Contacts, you
> need to work with the XML directly.
>
> What other operations do you need to perform?
>
> Cheers,
> Julian.
>
> On Nov 6, 7:32 am, rchrd <rchr...@gmail.com> wrote:
>
> > help!
>
> > I was able to have a success login using ClientLogin but what will I
> > do next? I'm onhttps://www.google.com/accounts/ClientLoginHowcan i

rat86

unread,
Jan 8, 2009, 12:04:58 PM1/8/09
to Google Contacts API
resolved with:
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/
default/full');


On 8 Gen, 17:40, rat86 <caputo.nic...@libero.it> wrote:
> i have tried this code, bit receive this error:
>
> Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with
> message 'Expected response code 200, got 403 <HTML> <HEAD>
> <TITLE>Cannot request contacts belonging to another user</TITLE> </
> HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Cannot request
> contacts belonging to another user</H1> <H2>Error 403</H2> </BODY> </
> HTML> ' in /home/rat86/public_html/Gmail/library/Zend/Gdata/App.php:
> 644 Stack trace: #0 /home/rat86/public_html/Gmail/library/Zend/
> Gdata.php(206): Zend_Gdata_App->performHttpRequest('GET', 'http://www.goog...', Array, NULL, NULL, NULL) #1 /home/rat86/public_html/
> Gmail/library/Zend/Gdata/App.php(782): Zend_Gdata->performHttpRequest
> ('GET', 'http://www.goog...', Array) #2 /home/rat86/public_html/Gmail/
> library/Zend/Gdata/App.php(686): Zend_Gdata_App->get('http://www.goog...', Array) #3 /home/rat86/public_html/Gmail/library/Zend/
> >http://framework.zend.com/apidoc/core/Zend_Gdata/Zend_Gdata_Feed.html...

Brian

unread,
Mar 6, 2009, 4:07:58 PM3/6/09
to Google Contacts API
Thanks SO MUCH for your help on this, everyone - I've been having so
much trouble figuring out how to do this without any Zend Contacts
API, I can't tell you. I'm using the code that Kris provided above
but can't seem to get any further at all, meaning - I can't get
anything other than the one email extension to save. However I try, I
can't replicate this procedure for phoneNumber, postalAddress, a
second email address, phone numbers or anything .. someone PLEASE show
me the key to figuring this out. Different refs, uris, array types?
Grr ..

Here's the source ..

// this section works great - thanks, Kris!

$extension = new Zend_Gdata_App_Extension_Element('email', null,
'http://schemas.google.com/g/2005');
$attributes['address'] = array('name'=>'address', 'value' =>
$homeemail);
$attributes['rel'] = array('name'=>'rel', 'namespaceUri'=>null,
'value' => 'http://schemas.google.com/g/2005#home');
$attributes['primary'] = array('name'=>'primary', 'namespaceUri' =>
null, 'value' => 'true');
$extension->setExtensionAttributes($attributes);

// this is me trying to add a second email addy, as well .. however,
only one email ends up getting saved to gbase.

$extension1 = new Zend_Gdata_App_Extension_Element('email', null,
'http://schemas.google.com/g/2005');
$attributes1['address'] = array('name'=>'address', 'value' =>
$workemail);
$attributes1['rel'] = array('name'=>'rel', 'namespaceUri'=>null,
'value' => 'http://schemas.google.com/g/2005#work');
$attributes1['primary'] = array('name'=>'primary', 'namespaceUri' =>
null, 'value' => 'false');
$extension1->setExtensionAttributes($attributes1);

// this is me trying to add a postal address ..

$extension2 = new Zend_Gdata_App_Extension_Element('postalAddress',
null, 'http://schemas.google.com/g/2005');
$attributes2['address'] = array('name'=>'text', 'value' =>
$theiraddy);
$attributes2['rel'] = array('name'=>'rel', 'namespaceUri'=>null,
'value' => 'http://schemas.google.com/g/2005#home');
$attributes2['primary'] = array('name'=>'primary', 'namespaceUri' =>
null, 'value' => 'true');
$extension2->setExtensionAttributes($attributes2);

// here's the actual writing bit

$entry = $gdata->newEntry();
$entry->title = $gdata->newTitle($name);
$entry->setExtensionElements(array($extension));
$entryResult = $gdata->insertEntry($entry,$mystream);

Which leads me to another big question .. how do I add multiple
extensions?

// $entry->setExtensionElements(array($extension),array
($extension1),array($extension2));

Doesn't seem to work. Again, thanks to everyone who has participated
in this thread so far - this is the ONLY place on the web to find this
particular nugget of information at the moment. Until the Zend
contacts API is out, this thread will probably be THE resource for
people trying to figure it out on their own. Was for me. :).

Andre Roy

unread,
Mar 7, 2009, 10:00:34 PM3/7/09
to google-co...@googlegroups.com
Brian wrote:
> Thanks SO MUCH for your help on this, everyone - I've been having so
> much trouble figuring out how to do this without any Zend Contacts
> API, I can't tell you. I'm using the code that Kris provided above
> but can't seem to get any further at all, meaning - I can't get
> anything other than the one email extension to save. However I try, I
> can't replicate this procedure for phoneNumber, postalAddress, a
> second email address, phone numbers or anything .. someone PLEASE show
> me the key to figuring this out. Different refs, uris, array types?
> Grr ..
>
Yeah, it's pretty confusing, that's for sure. Especially when
transferring from one system that returns contacts as one big long easy
to parse string and going to this objectmodel. Constantly shifting gears ...


Here's what I have, sort of a paraphrase of what I have actually ......

In essences, the actual update is the same as just a plain contact, you
just have to add the phone numbers and such to the entry. Here's what
I've been using. For about three days of testing now :)

A function here called AdBkEntryValue() parses a value out of the return
set (called <entry>) from my source system, just know that it returns a
string (possibly with newlines in it) of something reasonable here or an
empty string if there's no value in the source.

// Create the contact entry that we will populate
ContactEntry newContact = new ContactEntry();

// Throw the display name into there. The assumption is
// that the display name is what the contact is known as.
newContact.Title.Text = AdBkEntryValue("cn", entry);

// Create the email property and add it. Set relationship
// to be IsOther for all addresses, since Mirapoint only
// has one kind of email per contact. And since it's the
// only address, set it to be the primary one.
EMail newEmail = new EMail(AdBkEntryValue("mail", entry));
newEmail.Primary = true;
newEmail.Rel = ContactsRelationships.IsOther;
newContact.Emails.Add(newEmail);

// if there's a postal address, add that, too. Relationship
// and primary is set like for the email address.
t = AdBkEntryValue("postaladdress", entry);
if (t != "")
{
PostalAddress newPostal = new PostalAddress();

// Also add in things like City, Province etc that Mirapoint
// splits out but Google throws into one big box.
t += "\r\n";
t += AdBkEntryValue("l", entry) + " "; // city
t += AdBkEntryValue("st", entry) + " "; // prov
t += AdBkEntryValue("postalcode", entry) + "\r\n"; // postal
t += AdBkEntryValue("c", entry); // countty
newPostal.Value = t;
newPostal.Primary = true;
newPostal.Rel = ContactsRelationships.IsOther;
newContact.PostalAddresses.Add(newPostal);
}


// Assorted stuff. Just text.
// Also throw in the anniversary and birthdates here.
t = AdBkEntryValue("description", entry);
// these next two return any date in there if needed
t += AdBkEntryDate("anniversary", entry);
t += AdBkEntryDate("birth", entry);
t += "\r\nWebsite: ";
t += AdBkEntryValue("homeurl", entry);
if (t != "")
{
newContact.Content.Content = t;
}


// Phone numbers, how they translate
// Mirapoint Name Primary true if Google Name
// -------------------------- --------------- -----------
// telephonenumber: primaryphone=0 Work
// homephone: primaryphone=1 Home
// mobile: primaryphone=2 Mobile
// pager: primaryphone=3 Pager
// facsimiletelephonenumber: primaryphone=4 Work Fax
// n/a Home Fax
// n/a Other
//
// "telephonenumber"/"Work", primaryphone=0
t = AdBkEntryValue("telephonenumber", entry);
if (t != "")
{
PhoneNumber p = new PhoneNumber(t);
p.Label = "Work";
p.Primary = (AdBkEntryValue("primaryphone", entry)=="0");
newContact.Phonenumbers.Add(p);
}

// "homephone"/"Home", primaryphone=1
t = AdBkEntryValue("homephone", entry);
if (t != "")
{
PhoneNumber p = new PhoneNumber(t);
p.Label = "Home";
p.Primary = (AdBkEntryValue("primaryphone", entry)=="1");
newContact.Phonenumbers.Add(p);
}

// "mobile"/"Mobile", primaryphone=2
t = AdBkEntryValue("mobile", entry);
if (t != "")
{
PhoneNumber p = new PhoneNumber(t);
p.Label = "Mobile";
p.Primary = (AdBkEntryValue("primaryphone", entry)=="2");
newContact.Phonenumbers.Add(p);
}

// "pager"/"Pager", primaryphone=3
t = AdBkEntryValue("pager", entry);
if (t != "")
{
PhoneNumber p = new PhoneNumber(t);
p.Label = "Pager";
p.Primary = (AdBkEntryValue("primaryphone", entry)=="3");
newContact.Phonenumbers.Add(p);
}

// the "facsimiletelephonenumber"/"Work Fax", primaryphone=4
t = AdBkEntryValue("facsimiletelephonenumber", entry);
if (t != "")
{
PhoneNumber p = new PhoneNumber(t);
p.Label = "Work Fax";
p.Primary = (AdBkEntryValue("primaryphone", entry)=="4");
newContact.Phonenumbers.Add(p);
}

// Fill in the Organization and Job Title fields, if there.
// First see if the two together have anything, if yes
// then build the entry and add it.
t = AdBkEntryValue("o", entry) + AdBkEntryValue("title", entry);
if (t != "")
{
Organization newOrganization = new Organization();
t = AdBkEntryValue("o", entry);
if (t != "")
{
newOrganization.Name = t;
}

t = AdBkEntryValue("title", entry);
if (t != "")
{
newOrganization.Title = t;
}

// No idea what this line does. But it doesn't
// break and it's in the sample code.
newOrganization.Rel =
"http://schemas.google.com/g/2005#other";
newContact.Organizations.Add(newOrganization);
}


// Add the contact to the group. Actually, we add the group
// to the contact, but the effect is the same. First, get the
// category name by grabbing the number and then translating.
// And we don't do the "Unfiled" group, because it's Unfiled.
t = AdBkEntryValue("category", entry).ToUpper();
// if the length returned is pretty big, assume that the
// contact group is specified by name, otherwise, translate
// the numericreference Mirapoint uses to the name the user has
if ( t.Length < 6 ) // unlikely anyone has > 99999 groups
{
t = CategoryNumberToName(MiraGroupList, t).ToUpper();
}
if (t != "UNFILED")
{
// go though the groups list in google and see if
// we get a match by name, case-insensitive
for (int i = 0; i < GoogGroupList.Length; i++)
{
// if we have a match, add the group.
if (GoogGroupList[i].Title.Text.ToUpper() == t)
{
GroupMembership GrpMbr = new GroupMembership();
GrpMbr.HRef = GoogGroupList[i].Id.AbsoluteUri;
newContact.GroupMembership.Add(GrpMbr);
}
}
}


And then here, you just insert the contact with all the information
you've added to it into Google.

HTH.

--
Andre Roy -- If that don't fix it, then it's still broke.

andreroy55

unread,
Mar 7, 2009, 10:04:18 PM3/7/09
to Google Contacts API
Oy, just realized you said PHP and this is C# ... talk about
confusion... :/




On Mar 7, 10:00 pm, Andre Roy <andrero...@gmail.com> wrote:
> Brian wrote:
> > Thanks SO MUCH for your help on this, everyone - I've been having so
> > much trouble figuring out how to do this without any Zend Contacts
> > API, I can't tell you.  I'm using the code that Kris provided above
> > but can't seem to get any further at all, meaning - I can't get
> > anything other than the one email extension to save.  However I try, I
> > can't replicate this procedure for phoneNumber, postalAddress, a
> > second email address, phone numbers or anything .. someone PLEASE show
> > me the key to figuring this out.  Different refs, uris, array types?
> > Grr ..
>
> Yeah, it's pretty confusing, that's for sure. Especially when
> transferring from one system that returns contacts as one big long easy
> to parse string and going to this objectmodel. Constantly shifting gears ...
>
> Here's what I have, sort of a paraphrase of what I have actually ......
>
> In essences, the actual update is the same as just a plain contact, you
> just have to add the phone numbers and such to the entry. Here's what
> I've been using. For about three days of testing now :)
>
> A function here called AdBkEntryValue() parses a value out of the return
> set (called <entry>) from my source system, just know that it returns a
> string (possibly with newlines in it) of something reasonable here or an
> empty string if there's no value in the source.
>
>          // Create the contact entry that we will populate
>          ContactEntry newContact = new ContactEntry();
>
etc etc etc

brian

unread,
Mar 9, 2009, 9:52:36 AM3/9/09
to Google Contacts API
Thanks, Andreroy55 .. unfortunately I was looking for a PHP/Zend
snippet. However, it has been useful to see the different ways to
treat different data (phone, email, org, etc). Can I use these
newOrganization, newPhoneNumber and such in php/zend? I haven't been
able to, yet.

Anyone else out there with a little help to offer? Would WAY
appreciate it, as I'm still stumped on this one. Can only write to
email and content fields successfully - not to title, org, ph, addy,
etc.

Thanks!
Brian

Julian (Google)

unread,
Mar 10, 2009, 10:04:21 AM3/10/09
to Google Contacts API
Hi,

The following will add an email address to the entry. The idea is to
get the ExtensionElements array from the entry, add the new extension
and set the new array back to the entry.

// gets the entry's exenstions elemensts array
$extensionElements = $entry->getExtensionElements();

// creates the new email extension element
$extension = new Zend_Gdata_App_Extension_Element('email', null,
'http://schemas.google.com/g/2005');
$attributes['address'] = array
('namespaceUri'=>null,'name'=>'address', 'value' =>
'em...@domain.com');
$attributes['rel'] = array('namespaceUri'=>null,'name'=>'rel',
'value' => 'http://schemas.google.com/g/2005#home');
$attributes['primary'] = array
('namespaceUri'=>null,'name'=>'primary', 'value' => 'false'); // only
one Primary email
$extension->setExtensionAttributes($attributes);

// adds the new email extension element to the entry's exenstions
elemensts array
array_push( $extensionElements, $extension );
$entry->setExtensionElements($extensionElements);
$entry->save();

Cheers,
Julian.

andyjimmy

unread,
Mar 16, 2009, 7:50:41 AM3/16/09
to Google Contacts API
I too like brian am struggling to make it add more than one
'extension' at a time. And this would seem to be the only resource out
there on the zend framework.

I have used the same code as brian for the multiple extensions, but
the only entry that is added is the last on in the list, is there a
way to save them as you go through them, or is there some way to batch
add them all.

Also having alittle difficulty with adding phone number and postal
address... its the 'text()' field http://code.google.com/apis/gdata/elements.html#gdPhoneNumber
as referred to here that i don't know how to get working with zend

WBL Hockey

unread,
Mar 24, 2009, 1:53:50 AM3/24/09
to google-co...@googlegroups.com
Andy, I was able to get it to work (thanks to Julian's help) .. here's the golden code.  Postal and Phone had an extra array declaration step and the extensions need to have an array_push applied to stack them - wouldn't have ever figured either issue out without Julian's assistance, so thanks again JT.
 
 $extension = new Zend_Gdata_App_Extension_Element('email', null, 'http://schemas.google.com/g/2005');
 $attributes['address'] = array('namespaceUri'=>null,'name'=>'address', 'value' => $emailaddy);
 $attributes['rel'] = array('namespaceUri'=>null,'name'=>'rel', 'value' => 'http://schemas.google.com/g/2005#home');
 $attributes['primary'] = array('namespaceUri'=>null,'name'=>'primary', 'value' => 'true');
 $extension->setExtensionAttributes($attributes);
 
 $entry = $gdata->newEntry();
 $entry->title = $gdata->newTitle($name);
 $entry->setExtensionElements(array($extension));
 
    $extensionElements = $entry->getExtensionElements();
    $extension2 = new Zend_Gdata_App_Extension_Element('postalAddress', null, 'http://schemas.google.com/g/2005');
    $attributes2 = array();
    $extension2->setText($contactAddress);
    $attributes2['rel'] = array('namespaceUri'=>null,'name'=>'rel', 'value' => 'http://schemas.google.com/g/2005#home');
    $attributes2['primary'] = array('namespaceUri'=>null,'name'=>'primary', 'value' => 'true');
    $extension2->setExtensionAttributes($attributes2);
 
    array_push($extensionElements, $extension2);
    $entry->setExtensionElements($extensionElements);
 
    $extensionElements = $entry->getExtensionElements();
    $extension3 = new Zend_Gdata_App_Extension_Element('phoneNumber', null, 'http://schemas.google.com/g/2005');
    $attributes3 = array();
    $extension3->setText($phone);
    $attributes3['rel'] = array('namespaceUri'=>null,'name'=>'rel', 'value' => 'http://schemas.google.com/g/2005#home');
    $attributes3['primary'] = array('namespaceUri'=>null,'name'=>'primary', 'value' => 'true');
    $extension3->setExtensionAttributes($attributes3);
 
    array_push($extensionElements, $extension3);
    $entry->setExtensionElements($extensionElements);
 
 $content = $gdata->newContent($comments);
 $content->setType('text');
 $entry->content = $content;
 $entryResult = $gdata->insertEntry($entry,$mystream);
Hope that helps everyone!
Brian.

andyjimmy

unread,
Mar 26, 2009, 5:58:03 AM3/26/09
to Google Contacts API
Brian you are a genius!! Thanks very much also to Julian who inspired
the solution. I'm well on my way now to getting this badboy fully
operational!

andyjimmy

unread,
Mar 30, 2009, 1:45:27 AM3/30/09
to Google Contacts API
I have been trying to put my contacts into groups as they are being
added to google contacts with very little success. Do you need to have
a group create first before you add contacts into that group, or does
the API create them on the fly as you add contacts?

I have tried using extensions to add contacts to a group

$extensionElements = $entry->getExtensionElements();
$extension7 = new Zend_Gdata_App_Extension_Element
('gContact',null, 'http://schemas.google.com/contact/2008');
$attributes7 = array();
$extension7->setText('WSAP');
$extension7->setExtensionAttributes($attributes7);

array_push($extensionElements, $extension7);
$entry->setExtensionElements($extensionElements);

which doesn't have any affect on the contact information. The Contact
group WSAP does already exist in my contacts list.

Also how do you create a new group.

Thanks alot!!
Andrew

Sebastian

unread,
Mar 30, 2009, 8:23:05 AM3/30/09
to Google Contacts API
Hi everyone,

I'm new to Contacts API, and I'm a bit confused.

I managed to register my domain, and to get it granted, so that it
returns me a url with a TOKEN.

https://www.google.com/accounts/AuthSubRequest?scope=http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2F&session=1&secure=0&next=http%3A%2F%2Fallmybookings.com&hd=default

I don't know what I should do next. I use PHP, and in the google
developer's guide, says we should use this:

GET /accounts/AuthSubSessionToken HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: AuthSub token="yourAuthToken"
User-Agent: Java/1.5.0_06
Host: www.google.com
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

Is this PHP?

How do I implement this on my webpage?

I don't see any of your codes containing this.

Should I use $_GET['token'] to get the token?

Any guide would be appreciated,

Sebastian

Ashvin Savani

unread,
Mar 30, 2009, 4:37:03 PM3/30/09
to Google Contacts API
Sebastian,

Use Curl library for PHP. That should help.

Regards,

Ashvin

On Mar 30, 5:23 pm, Sebastian <sebast...@comocomo.com> wrote:
> Hi everyone,
>
> I'm new to Contacts API, and I'm a bit confused.
>
> I managed to register my domain, and to get it granted, so that it
> returns me a url with a TOKEN.
>
> https://www.google.com/accounts/AuthSubRequest?scope=http%3A%2F%2Fwww...

sebastian de comocomo

unread,
Mar 31, 2009, 4:12:20 AM3/31/09
to google-co...@googlegroups.com
Hi Ashvin,

I already check this:

http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_f
onje

Is there a specific section for Curl & PHP that I don't know of?

Could you send me the link?

Thanks,
Sebastian

Ashvin Savani

unread,
Mar 31, 2009, 6:11:43 AM3/31/09
to Google Contacts API
I don't remember the exact link but you can do google with curl php
class and you should get it whatever you need. But its low level and
bit hard path as you need to make your own api.

Regards,

Ashvin

On Mar 31, 1:12 pm, sebastian de comocomo <sebast...@comocomo.com>
wrote:
> Hi Ashvin,
>
> I already check this:
>
> http://code.google.com/apis/ajaxsearch/documentation/reference.html#_...

darie nicolae

unread,
Apr 15, 2009, 8:40:39 AM4/15/09
to Google Contacts API
Hello, it worked for me , but i tried to add a new extension,
groupMemberShipInfo and it doesnt work, this is my code :

$extension = new Zend_Gdata_App_Extension_Element('email', null,
'http://schemas.google.com/g/2005');

$attributes['address'] = array
('namespaceUri'=>null,'name'=>'address','value' => $contactAddress);
$attributes['rel'] = array('namespaceUri'=>null,'name'=>'rel',
'value' => 'http://schemas.google.com/g/2005#home');
$attributes['primary'] = array
('namespaceUri'=>null,'name'=>'primary', 'value' => 'true');

$extension->setExtensionAttributes($attributes);

$entry = $gdata->newEntry();
$entry->title = $gdata->newTitle($contactName);
$entry->setExtensionElements(array($extension));

$extensionElements = $entry->getExtensionElements();
$extension2 = new Zend_Gdata_App_Extension_Element
('postalAddress',null, 'http://schemas.google.com/g/2005');

$attributes2 = array();
$extension2->setText($contactPostal);
$attributes2['rel'] = array('namespaceUri'=>null,'name'=>'rel',
'value' => 'http://schemas.google.com/g/2005#home');
$attributes2['primary'] = array
('namespaceUri'=>null,'name'=>'primary','value' => 'true');
$extension2->setExtensionAttributes($attributes2);

array_push($extensionElements, $extension2);
$entry->setExtensionElements($extensionElements);

$extensionElements = $entry->getExtensionElements();
$extension3 = new Zend_Gdata_App_Extension_Element
('phoneNumber', null, 'http://schemas.google.com/g/2005');
$attributes3 = array();
$extension3->setText($contactPhone);
$attributes3['rel'] = array('namespaceUri'=>null,'name'=>'rel',
'value' => 'http://schemas.google.com/g/2005#home');
$attributes3['primary'] = array
('namespaceUri'=>null,'name'=>'primary','value' => 'true');
$extension3->setExtensionAttributes($attributes3);

array_push($extensionElements, $extension3);
$entry->setExtensionElements($extensionElements);


$extensionElements = $entry->getExtensionElements();
$extension4 = new Zend_Gdata_App_Extension_Element
('groupMembershipInfo', null, 'http://schemas.google.com/g/ 2005');
$attributes4 = array();
$attributes4['deleted'] = array
('namespaceUri'=>null,'name'=>'deleted', 'value' => 'false');
$attributes4['href'] = array
('namespaceUri'=>null,'name'=>'href','value' => $grouplink);

$extension4->setExtensionAttributes($attributes4);
array_push( $extensionElements, $extension4 );
$entry->setExtensionElements( $extensionElements );


$content = $gdata->newContent($contactNotes);
$content->setType('text');
$entry->content = $content;
$entryResult = $gdata->insertEntry($entry,"http://www.google.com/
m8/feeds/contacts/varzariu.nicolae%40gmail.com/full");

$grouplink is the variable which holds the group link to which i want
to add the contact.

If anybody did this, adding a contact to a certain group , please let
me know.

Regards,
Nicolae
> > > I too like brian am struggling to make itaddmore than one
> > > 'extension' at a time. And this would seem to be the only resource out
> > > there on the zend framework.
>
> > > I have used the same code as brian for the multiple extensions, but
> > > the only entry that is added is the last on in the list, is there a
> > > way to save them as you go through them, or is there some way to batch
> > >addthem all.

Sebastian

unread,
May 14, 2009, 8:14:10 AM5/14/09
to Google Contacts API
Hi,

sorry but I still don't understand how to make this work.

I just want to get a list of my contacts, (picture, email, name,
address, gender, phone number, lat & lng)

I get this message:

No input file specified.

1st.) I'm granted access
2nd.) it sends me to the page with the Token:

http://allmybookings.com/viewcontacts.php?token=CK3iteHqChDTl6Hf______8B

3rd.) I get the message: "No input file specified.".

In the config. I specified "viewcontacts.php". So, I don't understand.

I tried all the example codes in this dicussion group "Contact API for
PHP",

I just put <? in the front and ?> at the end to make it PHP.

I put my gmail email, and it's password.

am I suppose to do something else?

Could someone post a full code, in which I just need to change my
email & password?

<?

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Gdata_Query');

// Using Client Login
$client = Zend_Gdata_ClientLogin::getHttpClient
("seba...@gmail.com","MYPASSWORD", "cp");
$gdata = new Zend_Gdata($client);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/
contacts/
default/full');

$query->setMaxResults(100);
$feed = $gdata->getFeed($query);

foreach ($feed as $entry) {
$XMLContent = $entry->getXML();
print_r( $XMLContent );

}
?>

Thanks
Sebastian

Julian (Google)

unread,
May 15, 2009, 11:46:17 AM5/15/09
to Google Contacts API
Hi Sebastian,

You can try this example:

<?php

$clientLibraryPath = '/var/www/contacts/Zend';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR .
$clientLibraryPath);

require_once 'Loader.php';

Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Gdata_Query');

// Using Client Login
$client = Zend_Gdata_ClientLogin::getHttpClient("us...@domain.com",
"PASSWORD", "cp");
$gdata = new Zend_Gdata($client);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/
user%40domain.com/full');
$query->setMaxResults(1000);
$feed = $gdata->getFeed($query);

$xml = new SimpleXMLElement($feed->getXML());
$entries = $xml->children('http://www.w3.org/2005/Atom');

foreach ($entries->entry as $entry ) {
$defaults = $entry->children('http://schemas.google.com/g/2005');
echo 'title: '. $entry->title;
if ( isset($defaults->email)){
echo ' email: '. isset($defaults->email->attributes()->address);
}
echo ' address: '.$defaults->postalAddress;
}
?>

--Julian
> ("sebab...@gmail.com","MYPASSWORD", "cp");

sebastian de comocomo

unread,
May 15, 2009, 12:13:36 PM5/15/09
to google-co...@googlegroups.com
Hi Julian,

Could you check it out?

http://allmybookings.com/googlecontactsinfo.php

You need to click on the top link

Then you grand access,

And then it goes to:

http://allmybookings.com/viewcontacts.php?token=CK3iteHqChDZ_52o______8B

But I get a white page,

What am I doing wrong? I used your new code, with my email
"seba...@gmail.com" and it's password.

Thanks
Sebastian

sebastian de comocomo

unread,
May 16, 2009, 7:00:26 AM5/16/09
to google-co...@googlegroups.com
Hi Julian

I contacted my supplier one.com and they say they don't support ZEND, so I
suppose that is the problem.

Is there a way around it?

Can I download the Loader.php, and place it in my server?

Which other things would I need?

I visited zend.com and they give the option to install a zend server, this
seems very complex to me though.

Any ideas, suggestions would be really appreciated,

Sebastian


On 15/05/09 17:46, "Julian (Google)" <j...@google.com> wrote:

>

sebastian de comocomo

unread,
May 16, 2009, 7:55:13 AM5/16/09
to google-co...@googlegroups.com
Hi,

I tried this code, from the developers_guide
http://code.google.com/apis/contacts/docs/1.0/developers_guide_js.html

It says we can see Public feeds without any authentication

My supplier doesn't have ZEND

It also says " Retrieving contacts using query parameters"

I'd like to retrieve public profiles based on a query.

Does anyone have a full example from a-z?

Do I need to download any scripts? Ex. Like the picker.js ?

I've tried many things, and I get a white page.

Even the simplest example in the developers_guide doesn't work?

What am I doing wrong, am a bit frustrated as you can see. Too many tests to
find out my supplier didn't have ZEND. I didn't even know I needed that. I
didn't have it in my local host neither. So imagine ;o)

Thanks
sebastian


<html>
<head>

<script type="text/javascript"
src="http://www.google.com/jsapi">
</script>


<script>

google.load("gdata", "1.x");

google.setOnLoadCallback(initFunc);


/*
* Retrieve all contacts
*/

// Create the contacts service object
var contactsService =
new google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');

// The feed URI that is used for retrieving contacts
var feedUri =
'http://www.google.com/m8/feeds/contacts/seba...@gmail.com/full';

// I ALSO TRIED WITH DEFAULT

var query = new google.gdata.contacts.ContactQuery(feedUri);

// Set the maximum of the result set to be 50
query.setMaxResults(50);

// callback method to be invoked when getContactFeed() returns data
var callback = function(result) {

// An array of contact entries
var entries = result.feed.entry;

// Iterate through the array of contact entries
for (var i = 0; i < entries.length; i++) {
var contactEntry = entries[i];

var emailAddresses = contactEntry.getEmailAddresses();

// Iterate through the array of emails belonging to a single contact
entry
for (var j = 0; j < emailAddresses.length; j++) {
var emailAddress = emailAddresses[j].getAddress();
PRINT('email = ' + emailAddress);
}
}
}

// Error handler
var handleError = function(error) {
PRINT(error);
}

// Submit the request using the contacts service object
contactsService.getContactFeed(query, callback, handleError);
</script>
</head>
<body>








On 15/05/09 17:46, "Julian (Google)" <j...@google.com> wrote:

>

Julian (Google)

unread,
May 18, 2009, 9:42:43 AM5/18/09
to Google Contacts API
Hi,

If you are not able to use Zend, you could work directly with Curl in
PHP using the protocol:
http://code.google.com/apis/contacts/docs/2.0/developers_guide_protocol.html

Unfortunately I don't have an easier out of the box example at hand,
but there are some examples in this thread:
http://groups.google.com/group/google-contacts-api/browse_thread/thread/f6dcf92775f8549/18baf98b53cb8269

Using the protocol directly may not be an easy task and I see that you
also tried the JavaScript client, you can find more information here:
http://code.google.com/apis/contacts/docs/1.0/developers_guide_js.html

--Julian


On May 16, 12:55 pm, sebastian de comocomo <sebast...@comocomo.com>
wrote:
> Hi,
>
> I tried this code, from the developers_guidehttp://code.google.com/apis/contacts/docs/1.0/developers_guide_js.html
> 'http://www.google.com/m8/feeds/contacts/sebab...@gmail.com/full';
> > $client = Zend_Gdata_ClientLogin::getHttpClient("u...@domain.com",
> >>>> Is there a specific section for Curl &PHPthat I don't know of?
>
> >>>> Could you send me the link?
>
> >>>> Thanks,
> >>>> Sebastian
>
> >>>> On 30/03/09 21:37, "Ashvin Savani" <ash...@avinashi.com> wrote:
>
> >>>>> Sebastian,
>
> >>>>> Use Curl library forPHP. That should help.
>
> >>>>> Regards,
>
> >>>>> Ashvin
>
> >>>>> On Mar 30, 5:23 pm, Sebastian <sebast...@comocomo.com> wrote:
> >>>>>> Hi everyone,
>
> >>>>>> I'm new to Contacts API, and I'm a bit confused.
>
> >>>>>> I managed to register my domain, and to get it granted, so that it
> >>>>>> returns me a url with a TOKEN.
>
> >>>>>>https://www.google.com/accounts/AuthSubRequest?scope=http%3A%2F%2Fwww...
>
> >>>>>> I don't know what I should do next. I usePHP, and in the google

sebastian de comocomo

unread,
May 18, 2009, 1:31:34 PM5/18/09
to google-co...@googlegroups.com
Many thanks Julian,

I'll try the curl,

Sebastian
Reply all
Reply to author
Forward
Message has been deleted
0 new messages