Create a simple contact with PHP (new)

2,344 views
Skip to first unread message

Jason McDonald

unread,
Sep 16, 2011, 12:09:00 PM9/16/11
to google-co...@googlegroups.com
Hello all, I've been trying to create a simple google contact with PHP, with name, address, email, phone number, and organization name. I've searched tirelessly through this forum and throughout the internet and have not found a php script that works for this that is up to date as of 9/2011. I'm thinking maybe the scripts I found used to work but don't work anymore? Will someone please advise me on a php script that will add a google contact via php?


The one script I found that came close only adds the phone number and comments, nothing else:

 $extension = new Zend_Gdata_App_Extension_Element('email', null, ' 
 $attributes['address'] = array('namespaceUri'=>null,'name'=>'address', 
'value' => $emailaddy); 
 $attributes['rel'] = array('namespaceUri'=>null,'name'=>'rel', 'value' => ' 
 $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', 
    $attributes2 = array(); 
    $extension2->setText($contactAddress); 
    $attributes2['rel'] = array('namespaceUri'=>null,'name'=>'rel', 'value' 
    $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, 
    $attributes3 = array(); 
    $extension3->setText($phone); 
    $attributes3['rel'] = array('namespaceUri'=>null,'name'=>'rel', 'value' 
    $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); 


###############################################################################


This script I found only seems to add the name, email, and company name, nothing else:

$doc  = new DOMDocument(); 
  $doc->formatOutput = true; 
  $entry = $doc->createElement('atom:entry'); 
  $entry->setAttributeNS('http://www.w3.org/2000/xmlns/' , 
   'xmlns:atom', 'http://www.w3.org/2005/Atom'); 
  $entry->setAttributeNS('http://www.w3.org/2000/xmlns/' , 
   'xmlns:gd', 'http://schemas.google.com/g/2005'); 
  $doc->appendChild($entry); 

  // add name element 
  $name = $doc->createElement('gd:name'); 
  $entry->appendChild($name); 
  $name1="Jason Mac"; 
  $fullName = $doc->createElement('gd:fullName', $name1); 
  $name->appendChild($fullName); 

  // add email element 
  $email = $doc->createElement('gd:email'); 
  $email1=$emailAddress; 
   $email->setAttribute('address' ,$email1); 
  $email->setAttribute('rel' ,'http://schemas.google.com/g/2005#home'); 
  $entry->appendChild($email); 

  // add org name element 
  $org = $doc->createElement('gd:organization'); 
  $org->setAttribute('rel' ,'http://schemas.google.com/g/2005#work'); 
  $entry->appendChild($org); 
  $orgNamer="my Org";
  $orgName = $doc->createElement('gd:orgName', $orgNamer); 
  $org->appendChild($orgName); 


  // add org name element 
  $Phone = $doc->createElement('gd:Phone'); 
  $Phone->setAttribute('rel' ,'http://schemas.google.com/g/2005#work'); 
  $entry->appendChild($Phone); 
  $phone1="7777777777"; 
   $PhoneName = $doc->createElement('gd:PhoneName', $phone1); 
  $Phone->appendChild($PhoneName); 

  // insert entry 
  $entryResult = $gdata->insertEntry($doc->saveXML(), 'http://www.google.com/m8/feeds/contacts/default/full'); 



######################################################################

Any suggestions on how to add the name, address, phone, email, and organization name for a contact would be greatly appreciated! (and if you know how to add multiple email addresses / multiple phone numbers that's a big bonus)


Thanks, Jason


Justin

unread,
Sep 16, 2011, 12:24:37 PM9/16/11
to google-co...@googlegroups.com
Hey Jason, I felt the same way.
I am finishing up my class that I wrote a wrapper around the Google Contacts Data Api that is also using Zend_framework. It's not a complete system but it might get you headed in the right direction. I will post when I am done. (not far away)

Vinay Thakker

unread,
Sep 16, 2011, 1:11:30 PM9/16/11
to google-co...@googlegroups.com
Are you saying that both scripts that you posted do not work?

Vinay, Dito

Jason McDonald

unread,
Sep 16, 2011, 2:05:21 PM9/16/11
to google-co...@googlegroups.com
Both scripts work partially, but they don't do the whole job. The first script will only add the phone number and comments, nothing else (even though it looks like it should add the other items), and the second script will only add the name, email, and company name (when it should be adding the other items too.

At the end of the day, I would like a script that can add the name, the address, the phone number, the email address, and the group name, all in one action, and as I mentioned any help would be really appreciated.

-Jason

Justin

unread,
Sep 16, 2011, 3:04:28 PM9/16/11
to google-co...@googlegroups.com
Hi Jason, Now I know this isn't an end-all-be-all solution, but works pretty good for me.

Small sample usage example
include_once ('../classes/class.Gmail.php');
$gmail = new gmail();
$gmail->setOption('email',    $user['gmailEmail']);
$gmail->setOption('password', $user['gmailPassword']);
$gmail->login();
          
$results = $gmail->getContacts("4000", date('Y-m-d'), $showDeleted);
$c       = $gmail->parseContactsFeed($results);



Hope this helps a little.

class.Gmail.php
Message has been deleted

Jason McDonald

unread,
Sep 16, 2011, 5:06:44 PM9/16/11
to google-co...@googlegroups.com
Justin, that works awesome! Thank you so so so much. And for me it is definitely and end-all solution as that is all I need. For anyone who wants to use Justin's script to create a contact, this is the script I wrote to use it:

(Side note: to save a few seconds of frustration, all birthdays and anniversaries have to be added with the format "1980-03-93" so that it goes Year-Month-Day)




<?php

$firstName="Jdog";
$middleName="Ed";
$lastName="Macdizzle";
$dob="1980-02-04";
$anniversary="2001-01-03";
$emailWork="ja...@work.com";
$emailPersonal="ja...@personal.com";
$homePhone="832-453-3545";
$mobilePhone="201-532-4583";
$workPhone="654-213-5354";
$workPhone2="789-654-1233";
$fax="555-445-3569";
$company="Jason INC";
$title="Big Boss";
$workCity="houston";
$workAddress="110 Smith St";
$workAddress2="Suite 203";
$workProvince="TX";
$workZipCode="77070";
$workCountry="USA";
$homeCity="Dallas";
$homeAddress="222 Smith";
$homeAddress2="PO BOX 234";
$homeProvince="TX";
$homeZipCode="75287";
$homeCountry="USA";



$contact = array("firstName" => $firstName, "middleName" => $middleName, "lastName" => $lastName, "dob" => $dob, "anniversary" => $anniversary, "emailWork" => $emailWork, "emailPersonal" => $emailPersonal, "homePhone" => $homePhone, "mobilePhone" => $mobilePhone, "workPhone" => $workPhone, "workPhone2" => $workPhone2, "fax" => $fax, "company" => $company, "title" => $title, "workCity" => $workCity, "workAddress" => $workAddress, "workAddress2" => $workAddress2, "workProvince" => $workProvince, "workZipCode" => $workZipCode, "workCountry" => $workCountry, "homeCity" => $homeCity, "homeAddress" => $homeAddress, "homeAddress2" => $homeAddress2, "homeProvince" => $homeProvince, "homeZipCode" => $homeZipCode, "homeCountry" => $homeCountry, "website" => $website, "blog" => $blog);

include_once ('class.Gmail.php');
$gmail = new gmail();
$gmail->setOption('email', $yourGmailUserName);
$gmail->setOption('password', $yourGmailPassword);
$gmail->login();
       
$results = $gmail->contact($contact, 'add');


?>





P.S. In order for the website and blog of the user to be added, you just have to add these few lines to the contact function inside of the class.Gmail.php file that Justin so kindly provided:




//    WEBSITE
if(!empty($contact['website'])) {
$webber = $doc->createElement('gContact:website');
$webber->setAttribute('rel', 'home');
$webber->setAttribute('href', $contact['website']);   

$entry->appendChild($webber);
}

//    BLOG
if(!empty($contact['website'])) {
$webberB = $doc->createElement('gContact:website');
$webberB->setAttribute('rel', 'blog');
$webberB->setAttribute('href', $contact['blog']);   

$entry->appendChild($webberB);
}





//Thanks again Justin!

Jason McDonald

unread,
Sep 16, 2011, 5:56:31 PM9/16/11
to google-co...@googlegroups.com
Also, one last thing, if you want to use the above script to add your contact to a contact group that is already in existence (the group has to already be made in gmail, i dont know how to make a group on the fly), this is what you do:

1. go to gmail, click on contacts, then click on the group you want to add the contact to, and look at the url in the browser address bar and you will see something like this 

"http://........../mail/#contacts/group/75605522099e2b79/Group+Name"

2. Make a mental note of the string in the url above that resembles: 75605522099e2b79 - this is your group ID

3. Add the following lines to the contact function that justin provided: 

//   GROUP
if(!empty($contact['group'])) {
$grouper = $doc->createElement('gContact:groupMembershipInfo');
 
$grouper->setAttribute('href', $contact['group']);   


$entry->appendChild($grouper);
}


4. Add the following variable to the script I used above to call the function (the string after 'base' is your group ID from above):

$group="http://www.google.com/m8/feeds/groups/YourGmailUserName%40gmail.com/base/75605522099e2b79";


5. Then make sure to add the new group variable that to your $contact array:

$contact = array("firstName" => $firstName, "middleName" => $middleName, "lastName" => $lastName, "dob" => $dob, "anniversary" => $anniversary, "emailWork" => $emailWork, "emailPersonal" => $emailPersonal, "homePhone" => $homePhone, "mobilePhone" => $mobilePhone, "workPhone" => $workPhone, "workPhone2" => $workPhone2, "fax" => $fax, "company" => $company, "title" => $title, "workCity" => $workCity, "workAddress" => $workAddress, "workAddress2" => $workAddress2, "workProvince" => $workProvince, "workZipCode" => $workZipCode, "workCountry" => $workCountry, "homeCity" => $homeCity, "homeAddress" => $homeAddress, "homeAddress2" => $homeAddress2, "homeProvince" => $homeProvince, "homeZipCode" => $homeZipCode, "homeCountry" => $homeCountry, "website" => $website, "blog" => $blog, "group" => $group);





6. Now when you run the script, it should add whatever contact you make to a particular group.




Justin

unread,
Sep 16, 2011, 6:44:21 PM9/16/11
to google-co...@googlegroups.com
Hi Jason,

Thank you for adding in the functionality of those extra pieces, in the future I will expand on the details with group etc.
That just added a whole lot more complexity that I didn't want to go through. haha.

Good luck mate!

Jason McDonald

unread,
Sep 17, 2011, 10:59:42 PM9/17/11
to google-co...@googlegroups.com
complexity, gotta love it :) i have one more question to wrap up my input in this, and it's kind of a last piece of the puzzle for me (albeit a fairly important one). Do you have any idea where to start when it comes to adding a note to the note field for a contact using the script you provided? I've spent the last 2 hours trying every possible combination and seem to hit a brick wall.


thanks and take care, jason

Justin Giesbrecht

unread,
Sep 17, 2011, 11:10:35 PM9/17/11
to google-co...@googlegroups.com

Hmm I thought it was in there docunentation. I'm not totally sure of the top of my head.

I will look when I'm in my office on monday.

> --
> 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

Jason McDonald

unread,
Sep 17, 2011, 11:11:47 PM9/17/11
to google-co...@googlegroups.com
ill definitely keep looking and will touch base with u if i cant find it. thanks man.

Justin Giesbrecht

unread,
Sep 17, 2011, 11:13:48 PM9/17/11
to google-co...@googlegroups.com

Great. If, you find a solution post it and I will update my code ... Cheers

On Sep 17, 2011 8:12 PM, "Jason McDonald" <anyand...@gmail.com> wrote:
> ill definitely keep looking and will touch base with u if i cant find it.
> thanks man.
>

Jason McDonald

unread,
Sep 17, 2011, 11:44:51 PM9/17/11
to google-co...@googlegroups.com
Alright I figured it out! My problem was I was trying to create gd:content when I should have created an atom:content.

Anyways, if you want to add text to the notes field for a contact entry, add this to the class.Gmail.php file:


 if(!empty($contact['notes'])) {
                
                $notes = $doc->createElement('atom:content', $contact['notes']);
$notes->setAttribute('type', 'text');

$entry->appendChild($notes);
               
               }

I'm sure my girlfriend is pissed that I'm still working on this at 10:40pm on a saturday night, but hey, that's just the price of victory. (of course i'm kidding i'm sure i'll have to make up for this by taking her to a movie tomorrow or some other equally painful sacrifice :P)


Jason McDonald

unread,
Sep 17, 2011, 11:47:20 PM9/17/11
to google-co...@googlegroups.com
Also, as a side-note for anyone in the future, I tried using a basic for loop to create multiple contacts at the same time with this script. I was able to create around 700 contacts (it varied each time) until finally getting an error. I'll probably get around this by making several independent calls to the script and limiting the number of contacts processed at one time to around 400 or so to be on the safe side.


Justin Giesbrecht

unread,
Sep 17, 2011, 11:50:37 PM9/17/11
to google-co...@googlegroups.com

You need  to use the batch processing... For every 100 requests. It equals 1 process . Which is another thing that needs to be added..., easy to implement though.

Justin Giesbrecht

unread,
Sep 18, 2011, 12:21:00 AM9/18/11
to google-co...@googlegroups.com

Now that I think about it. Thats strange with the 700 contacts because I uploaded 1400 with no problem. And then downloaded them no problem.... Hmm will havE add that batch processing sooner than I thought.

Justin

unread,
Sep 19, 2011, 1:05:50 PM9/19/11
to google-co...@googlegroups.com
Another thing that I just added was the websites, I forgot to re-add that when I posted this file. So here is the addition that I added after The anniversary section, Estimated line # was 306.

//    WEBSITES
            if(!empty($contact['websiteWork'])) {
            $website = $doc->createElement('gContact:website');
                       $website->setAttribute('rel', 'work');
                       $website->setAttribute('href', $contact['websiteWork']);
                       $entry->appendChild($website);
            }
            if(!empty($contact['websitePersonal'])) {
            $website = $doc->createElement('gContact:website');
                       $website->setAttribute('rel', 'home-page');
                       $website->setAttribute('href', $contact['websitePersonal']);
                       $entry->appendChild($website);
            }

Jason McDonald

unread,
Sep 19, 2011, 4:05:38 PM9/19/11
to google-co...@googlegroups.com
Thanks Justin, that's awesome. You'll definitely have to fill me in on the batch processing when you get a handle on it - that's definitely something that is pretty new to me. thanks again for all your help.

Gaurav Tomer

unread,
Oct 3, 2011, 4:37:35 AM10/3/11
to google-co...@googlegroups.com
can this class will work with 2-legged Oauth ... plz post some script if u can .... 
i will be very thankful to you 
you guys are doing a great job

Justin

unread,
Oct 3, 2011, 6:19:31 PM10/3/11
to google-co...@googlegroups.com
Hello Gaurav,

I have not developed this for that Oauth. Although I believe there is an example by IBM out there... search Google.
Google Contacts Api with Zend Framework IBM example

Hope that helps, sorry I don't have any samples.

Reply all
Reply to author
Forward
0 new messages