delete contact from group

164 views
Skip to first unread message

darie nicolae

unread,
Mar 28, 2009, 6:57:29 PM3/28/09
to Google Contacts API
Hello,

My question is how do i delete a contact from a group, through PHP?

Thanks.

Julian (Google)

unread,
Mar 30, 2009, 11:19:35 AM3/30/09
to Google Contacts API
Hi,

You can remove the appropriate Extension Element for the Entry, for
example the following code iterates over all Entries and removes them
from one Group:

$client = Zend_Gdata_ClientLogin::getHttpClient("us...@domain.com",
"PASWORD", "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,Zend_Gdata_App_Feed);

// Group ID to Remove:
$groupId = 'http://www.google.com/m8/feeds/groups/user%40domain.com/
base/7d60cf6309868fc6';

foreach ($feed->getEntry() as $entry ) {
$newExtensionElements = array();
foreach ( $entry->getExtensionElements() as $extensionElement ) {
$domElement = $extensionElement->getDom();
if ( !($domElement->tagName == 'groupMembershipInfo' and
$domElement->getAttribute( 'href' ) == $groupId )) {
array_push($newExtensionElements, $extensionElement );
}
}
#print_r( $newExtensionElements );
$entry->setExtensionElements( $newExtensionElements );
$entry->save();
}

You can find more information about Gdata_App_Base and
Gdata_App_Extension_Element in the following links:
http://framework.zend.com/apidoc/core/Zend_Gdata/App/Zend_Gdata_App_Base.html
http://framework.zend.com/apidoc/core/Zend_Gdata/App/Zend_Gdata_App_Extension_Element.html

Cheers,
Julian.

darie nicolae

unread,
Mar 30, 2009, 5:09:12 PM3/30/09
to Google Contacts API
Hey there, thanks for the quick response, ive tested the code but it
doesnt work! i got the main idea of how it works, but if i put echo
'something'; inside foreach ( $entry->getExtensionElements() as
$extensionElement ) , the echo doesnt get displayed, that means the
code doesnt get executed in that foreach.

Let me know if i do a mistake.
Thanks

On Mar 30, 5:19 pm, "Julian (Google)" <j...@google.com> wrote:
> Hi,
>
> You can remove the appropriate Extension Element for the Entry, for
> example the following code iterates over all Entries and removes them
> from one Group:
>
> $client = Zend_Gdata_ClientLogin::getHttpClient("u...@domain.com",
> "PASWORD", "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,Zend_Gdata_App_Feed);
>
> // Group ID to Remove:
> $groupId = 'http://www.google.com/m8/feeds/groups/user%40domain.com/
> base/7d60cf6309868fc6';
>
> foreach ($feed->getEntry() as $entry ) {
>   $newExtensionElements = array();
>   foreach ( $entry->getExtensionElements() as $extensionElement ) {
>     $domElement = $extensionElement->getDom();
>     if ( !($domElement->tagName == 'groupMembershipInfo' and
> $domElement->getAttribute( 'href' ) == $groupId )) {
>       array_push($newExtensionElements, $extensionElement );
>     }
>   }
>   #print_r( $newExtensionElements );
>   $entry->setExtensionElements( $newExtensionElements );
>   $entry->save();
>
> }
>
> You can find more information about Gdata_App_Base and
> Gdata_App_Extension_Element in the following links:http://framework.zend.com/apidoc/core/Zend_Gdata/App/Zend_Gdata_App_B...http://framework.zend.com/apidoc/core/Zend_Gdata/App/Zend_Gdata_App_E...

Julian (Google)

unread,
Mar 31, 2009, 5:27:53 AM3/31/09
to Google Contacts API
Hi,

Just in case, check that you have the path and the imports:

$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');
Zend_Loader::loadClass('Zend_Gdata_Query');

Maybe you can try a print_r( $entry->getExtensionElements() ); to
double check that there is information there.

-Julian
> > Gdata_App_Extension_Element in the following links:http://framework.zend.com/apidoc/core/Zend_Gdata/App/Zend_Gdata_App_B......

darie nicolae

unread,
Mar 31, 2009, 10:49:37 AM3/31/09
to Google Contacts API
The information is not there : this is my code :

<?php

$clientLibraryPath = '/Users/nicolaedarie/Sites/ZendGdata/library/';
set_include_path(get_include_path() . PATH_SEPARATOR .
$clientLibraryPath);

include($clientLibraryPath . '/Zend/Loader.php');

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

$username="us...@gmail.com";
$pass="pass";
$client = Zend_Gdata_ClientLogin::getHttpClient($username, $pass,
"cp");
$username = str_replace("@","%40",$username);
$gdata = new Zend_Gdata($client);
$query = new Zend_Gdata_Query( "http://www.google.com/m8/feeds/groups/
default/full");
$query->setMaxResults( 100 );
$feed = $gdata->getFeed( $query );

$groupId = 'http://www.google.com/m8/feeds/groups/varzariu.nicolae
%40gmail.com/base/2842a9b10e7d48bb';

foreach ($feed->getEntry() as $entry ) {
$newExtensionElements = array();

print_r($entry->getExtensionElements());

foreach ( $entry->getExtensionElements() as $extensionElement ) {

$domElement = $extensionElement->getDom();
echo 'tag name : ' . '<br>';

if ( !($domElement->tagName == 'groupMembershipInfo' and
$domElement->getAttribute( 'href' ) == $groupId) ) {
echo 'tag name : ' . $domElement->tagName;
array_push($newExtensionElements, $extensionElement );
}
}
print_r( $newExtensionElements );
$entry->setExtensionElements( $newExtensionElements );
$entry->save();
}

So why the $entry->getExtensionElements() is empty?

Regards,
Nicolae

?>

Julian (Google)

unread,
Apr 1, 2009, 12:15:51 PM4/1/09
to Google Contacts API
Hi Nicolae,

There is an error on your feed Url, to get the contacts feed it should
be "/contacts/" instead of "/groups/":

$query = new Zend_Gdata_Query( "http://www.google.com/m8/feeds/
contacts/default/full");

Please let me know if that does not solve the issue.

-Julian.

On Mar 31, 3:49 pm, darie nicolae <darie.nico...@gmail.com> wrote:
> The information is not there : this is my code :
>
> <?php
>
> $clientLibraryPath = '/Users/nicolaedarie/Sites/ZendGdata/library/';
> set_include_path(get_include_path() . PATH_SEPARATOR .
> $clientLibraryPath);
>
> include($clientLibraryPath . '/Zend/Loader.php');
>
> Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
> Zend_Loader::loadClass('Zend_Gdata_Gapps');
> Zend_Loader::loadClass('Zend_Gdata_Query');
>
> $username="u...@gmail.com";

darie nicolae

unread,
Apr 1, 2009, 3:31:31 PM4/1/09
to Google Contacts API
Yes, thats true, it worked, i should have seen it. One thing that it
is weird, it left one contact in the group, but maybe its something
small and i will fix it.

Thanks a lot again for your help!

Cheers

darie nicolae

unread,
Apr 1, 2009, 3:40:01 PM4/1/09
to Google Contacts API
Hmm, ive runned a test again, and instead of deleting , it added a
duplicate and they contacts were not deleted!

I think its still not running 100% correct the code.

andyjimmy

unread,
Apr 2, 2009, 6:02:59 AM4/2/09
to Google Contacts API
Great script for removing the groups. I haven't had any problems with
removing the group info. But haven't been able to put in group info.
I'm using zend like you have been doing and this is what i have so
far.

// group
$groupId = 'http://www.google.com/m8/feeds/groups/wsapyouth
%40gmail.com/base/7988c2040c9693c8';
$extensionElements = $entry->getExtensionElements();
$extension6= new Zend_Gdata_App_Extension_Element
('GroupMembershipinfo', null,'http://schemas.google.com/g/2005');
$attributes6['href'] = $groupId;

$extension6->setExtensionAttributes($attributes6);

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


any help would be greatly appreciated
thanks in advance!
Andrew

Julian (Google)

unread,
Apr 2, 2009, 10:52:53 AM4/2/09
to Google Contacts API
Hi Andrew,

Here is an example to add user to a group:

$extensionElements = $entry->getExtensionElements();
$extension = new Zend_Gdata_App_Extension_Element
('groupMembershipInfo', null, 'http://schemas.google.com/contact/
2008');
$attribures1 = array();
$attributes1['deleted'] = array
('namespaceUri'=>null,'name'=>'deleted', 'value' => 'false');
$attributes1['href'] = array('namespaceUri'=>null,'name'=>'href',
'value' => 'http://www.google.com/m8/feeds/groups/user%40domain.com/
base/7d60cf6309868fc6');
$extension->setExtensionAttributes($attributes1);
array_push( $extensionElements, $extension );
$entry->setExtensionElements( $extensionElements );
$entry->save();

-Julian

andyjimmy

unread,
Apr 3, 2009, 11:43:21 PM4/3/09
to Google Contacts API
Worked a charm Julian. Thank you very much!

Julian (Google)

unread,
Apr 6, 2009, 9:58:22 AM4/6/09
to Google Contacts API
You're welcome Andrew.

Because other people asked about .NET, here is an example in C# (using
the latest library). This removes a user from group using a group ID:

RequestSettings requestSettings = new RequestSettings("App-name",
"us...@domain.com", "PASSWORD");
requestSettings.AutoPaging = true;
ContactsRequest contactsRequest = new ContactsRequest
(requestSettings);
Uri uri = new Uri("http://www.google.com/m8/feeds/contacts/user
%40domain.com/full/1"); // User's ID
Contact contact = contactsRequest.Retrieve<Contact>(uri);

foreach (GroupMembership group in contact.GroupMembership){
// Group's ID
if ( group.HRef.Equals( "http://www.google.com/m8/feeds/groups/user
%40domain.com/base/6" )) {
contact.GroupMembership.Remove( group );
contactsRequest.Update<Contact>(contact);
break;

darie nicolae

unread,
Apr 6, 2009, 10:01:54 AM4/6/09
to Google Contacts API
Hey Julian, the code Andrew wrote above it was for removing a whole
group, the code you said to remove all contacts from a group actually
it didnt worked, it added a duplicate of the last contact instead and
nothing was deleted.

Do you know what could be the problem?

And also, how can i add phoneNumber and Notes to a contact when i add
it to a group ?( Talking about PHP )

Gracias!

On Apr 6, 3:58 pm, "Julian (Google)" <j...@google.com> wrote:
> You're welcome Andrew.
>
> Because other people asked about .NET, here is an example in C# (using
> the latest library). This removes a user from group using a group ID:
>
> RequestSettings requestSettings = new RequestSettings("App-name",
> "u...@domain.com", "PASSWORD");

Julian (Google)

unread,
Apr 6, 2009, 12:16:25 PM4/6/09
to Google Contacts API
Hi,

Similar to my previous post (http://groups.google.com/group/google-
contacts-api/browse_thread/thread/55ca554133da51ac), just by changing
the feed to a Groups feed. You can delete a whole Group(Entry) using
its Title like this:

<?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');
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/groups/
user%40domain.com/full');
$feed = $gdata->getFeed($query,Zend_Gdata_App_Feed);

foreach ($feed->getEntry() as $entry ) {
if ( $entry->getTitle()->getText() == "GROUP Title" )
$entry->delete();
}

?>

If you have the ID of the Group, you can get the entry directly and
delete it without iterating over the whole list.

Cheers,
-Julian

Julian (Google)

unread,
Apr 6, 2009, 12:53:35 PM4/6/09
to Google Contacts API
Hi,

Also, about the Phone Number and the Notes:

$entry->setContent( new Zend_Gdata_App_Extension_Content( 'New NOTE',
'text'));
$extension = new Zend_Gdata_App_Extension_Element('phoneNumber', null,
'http://schemas.google.com/g/2005');
$extension->setText('555-6789');
$attribures1 = array();
$attributes1['rel'] = array('namespaceUri'=>null,'name'=>'rel',
'value' => 'http://schemas.google.com/g/2005#home');
$extension->setExtensionAttributes($attributes1);
array_push( $extensionElements, $extension );
$entry->setExtensionElements( $extensionElements );
$entry->save();

The Relation for the phone number can be one of the following:
http://code.google.com/apis/gdata/elements.html#gdPhoneNumber

Cheers,
Julian

darie nicolae

unread,
Apr 6, 2009, 4:47:18 PM4/6/09
to Google Contacts API
Thanks Julian,

About removing a specific contact name from a group. the code above
was for removing an entire group by a specific group title, what i
want is to remove a specific contact name from a group X :

I did something like this, but it doesnt work :

$query = new Zend_Gdata_Query("http://www.google.com/m8/feeds/
contacts/
varzariu.nicolae%40gmail.com/full/?group=" . $grouplink . '&v=2');
$feed = $gdata->getFeed( $query );

foreach ($feed->getEntry() as $entry ) {

echo $entry->getTitle()->getText() . '<br>';

if ( $entry->getTitle()->getText() == "contact title")
$entry->delete();
}

$grouplink is the link of the group id.
the echo $entry->getTitle()->getText() displays the contact title,
but when i do $entry->delete it doesnt work.

Thanks and sorry for asking so many questions.

Julian (Google)

unread,
Apr 7, 2009, 6:02:04 AM4/7/09
to Google Contacts API
Hi,

Maybe you can try the following, it finds the contacts by its title
and removes the appropriate groupMembershipInfo for a given GroupID:

<?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');
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');
$feed = $gdata->getFeed($query,Zend_Gdata_App_Feed);

$contactTitle = 'Title';

foreach ($feed->getEntry() as $entry ) {
if ( $entry->getTitle() == $contactTitle ) {
$newExtensionElements = array();
foreach ( $entry->getExtensionElements() as $extensionElement ) {
$domElement = $extensionElement->getDom();
if ( !($domElement->tagName == 'groupMembershipInfo' and
$domElement->getAttribute( 'href' ) == $groupId )) {
array_push($newExtensionElements, $extensionElement );
}
}
$entry->setExtensionElements( $newExtensionElements );
$entry->save();
}
}
?>

-Julian

On Apr 6, 9:47 pm, darie nicolae <darie.nico...@gmail.com> wrote:
> Thanks Julian,
>
> About removing a specific contact name from a group. the code above
> was for removing an entire group by a specific group title, what i
> want is to remove a specific contact name from a group X :
>
> I did something like this, but it doesnt work :
>
> $query = new Zend_Gdata_Query("http://www.google.com/m8/feeds/
> contacts/
> varzariu.nicolae%40gmail.com/full/?group=" . $grouplink . '&v=2');
> $feed = $gdata->getFeed( $query );
>
> foreach ($feed->getEntry() as $entry ) {
>
>        echo $entry->getTitle()->getText() . '<br>';
>
>         if ( $entry->getTitle()->getText() == "contact title")
>                $entry->delete();
>
> }
>
> $grouplink is the link of the group id.
> the echo $entry->getTitle()->getText()  displays the contact title,
> but when i do $entry->deleteit doesnt work.
>
> Thanks and sorry for asking so many questions.
>
> On Apr 6, 6:53 pm, "Julian (Google)" <j...@google.com> wrote:
>
> > Hi,
>
> > Also, about the Phone Number and the Notes:
>
> > $entry->setContent( new Zend_Gdata_App_Extension_Content( 'New NOTE',
> > 'text'));
> > $extension = new Zend_Gdata_App_Extension_Element('phoneNumber', null,
> > 'http://schemas.google.com/g/2005');
> > $extension->setText('555-6789');
> > $attribures1 = array();
> > $attributes1['rel'] = array('namespaceUri'=>null,'name'=>'rel',
> > 'value' => 'http://schemas.google.com/g/2005#home');
> > $extension->setExtensionAttributes($attributes1);
> > array_push( $extensionElements, $extension );
> > $entry->setExtensionElements( $extensionElements );
> > $entry->save();
>
> > The Relation for the phone number can be one of the following:http://code.google.com/apis/gdata/elements.html#gdPhoneNumber
>
> > Cheers,
> > Julian
>
> > On Apr 6, 5:16 pm, "Julian (Google)" <j...@google.com> wrote:
>
> > > Hi,
>
> > > Similar to my previous post (http://groups.google.com/group/google-
> > > contacts-api/browse_thread/thread/55ca554133da51ac), just by changing
> > > the feed to a Groups feed. You candeletea whole Group(Entry) using
> > > its Title like this:
>
> > > <?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');
> > > 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/groups/
> > > user%40domain.com/full');
> > > $feed = $gdata->getFeed($query,Zend_Gdata_App_Feed);
>
> > > foreach ($feed->getEntry() as $entry ) {
> > >  if ( $entry->getTitle()->getText() == "GROUP Title" )
> > >    $entry->delete();
>
> > > }
>
> > > ?>
>
> > > If you have the ID of the Group, you can get the entry directly and
> > >deleteit without iterating over the whole list.

darie nicolae

unread,
Apr 15, 2009, 5:56:36 AM4/15/09
to Google Contacts API
Nope this didnt worked also, something is not okay, can you see if
there is a piece of code that works for sure when trying to remove a
contact from a specific group ?!

Thanks a lot.

On Apr 7, 12:02 pm, "Julian (Google)" <j...@google.com> wrote:
> Hi,
>
> Maybe you can try the following, it finds the contacts by its title
> and removes the appropriate groupMembershipInfo for a given GroupID:
>
> <?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');
> Zend_Loader::loadClass('Zend_Gdata_Query');
>
> // Using Client Login

Darie Nicolae

unread,
Apr 15, 2009, 8:59:57 AM4/15/09
to google-co...@googlegroups.com
This didnt worked.

darie nicolae

unread,
Apr 15, 2009, 9:02:17 AM4/15/09
to Google Contacts API
This didnt worked, i added extension like phoneNumber,postalAddress,
Notes, it worked, but for the groupMembershipInfo didnt worked, what
could be wrong?

Here is the piece of code :

$extensionElements = $entry->getExtensionElements();
$extension4 = new Zend_Gdata_App_Extension_Element
('groupMembershipInfo', null, 'http://schemas.google.com/contact/
2008');
$attributes4 = array();
$attributes4['deleted'] = array
('namespaceUri'=>null,'name'=>'deleted', 'value' => 'false');
$attributes4['href'] = array
('namespaceUri'=>null,'name'=>'href','value' => 'http://www.google.com/
m8/feeds/groups/varzariu.nicolae%40gmail.com/base/40a2e3420df476df');

$extension4->setExtensionAttributes($attributes4);

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

Please let me know if im wrong.

Cheers

darie nicolae

unread,
Apr 15, 2009, 9:10:06 AM4/15/09
to Google Contacts API
Hey i figured it out, i needed to add this :

$entryResult = $gdata->insertEntry($entry,"http://www.google.com/
m8/feeds/contacts/varzariu.nicolae%40gmail.com/full/?group=http://
www.google.com/m8/feeds/groups/varzariu.nicolae%40gmail.com/base/40a2e3420df476df&v=2");

It doenst work with the stream http://www.google.com/m8/feeds/contacts/varzariu.nicolae%40gmail.com/full
:)

Cheers!
Reply all
Reply to author
Forward
0 new messages