Friday , 24 May 2013
You are here: Home » Code » Retrieving all Google Apps Groups and Users within the Groups with PHP/Zend Framework
Retrieving all Google Apps Groups and Users within the Groups with PHP/Zend Framework

Retrieving all Google Apps Groups and Users within the Groups with PHP/Zend Framework

If you are using google apps, then you probably love groups as much as I do. Although, managing them can become somewhat of a hassle and there is no good way to get a listing of all groups and the users within all of those groups.

So, I wrote up some quick php code that will output all groups and users within those groups to a html file which can be viewed by anyone. (it defaults to allgroups-and-users.html in the code, but that can be changed).

Listing will look like:

  • GroupName
    • user1@domain.com
    • user2@domain.com
    • etc
  • AdditionalGroupName
    • user3@domain.com

In order to run this php code, you will need the Zend Gdata framework for Google Apps installed.

Here’s the code:


<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');

###### CHANGE THESE 3 LINES ######
$domain = "domaingoeshere.com";
$emailaddr = "adminaccount@domaingoeshere.com";
$emailpw = "passwordgoeshere";

$service = Zend_Gdata_Gapps::AUTH_SERVICE_NAME;

$client = Zend_Gdata_ClientLogin::getHttpClient($emailaddr, $emailpw, $service);
$gdata = new Zend_Gdata_Gapps($client, $domain);

#### CHANGE THIS LINE FOR ANOTHER OUTPUT NAME
$myfile = "allgroups-and-users.html";
$fh = fopen($myfile, 'w');

$stringData ="<head>";
fwrite($fh, $stringData);
$stringData ="<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">";
fwrite($fh, $stringData);
$stringData ="</head>";
fwrite($fh, $stringData);

$stringData ="<html><body>";
fwrite($fh, $stringData);
$stringData ="<h2>All Groups and Users</h2>";
fwrite($fh, $stringData);

$feed = $gdata->retrieveAllEmailLists();
foreach ($feed as $list) {
	$stringData = "<li><b>" . $list->emailList->name . "</b>";
		fwrite($fh, $stringData);
	$feed2 = $gdata->retrieveAllRecipients($list->emailList->name);
	$stringData = "<ul>";
		fwrite($fh, $stringData);
	  foreach ($feed2 as $recipient) {
	  $stringData = "<li>" . $recipient->who->email;
		fwrite($fh, $stringData);
	  }
	$stringData = "</ul>";
		fwrite($fh, $stringData);
}
$stringData = "</body>";
 fwrite($fh, $stringData);
fclose($fh);
?>

As you notice looking at the code, some variables have to be changed in order to suit your Google Apps domain, but its pretty straightforward. The code grabs all groups, stores in an array, then for each of those groups within the list, it grabs each of the users and prints to the file, and continues on.

Enjoy!

About Josh Gilmour

Sr. Systems & Network Administrator, trying to be blogger, living in Boston Massachusetts.

2 comments

  1. Thank you very much for this code, i’m using it and it’s very helpful.

  2. Awesome ! Just what I was looking for.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Scroll To Top
Descargar musica