Skip to content

Instantly share code, notes, and snippets.

@jonswaff
Created August 16, 2012 13:41
Show Gist options
  • Save jonswaff/3370165 to your computer and use it in GitHub Desktop.
Save jonswaff/3370165 to your computer and use it in GitHub Desktop.
Google Groups Provisioning API with PHP (Zend). Memory usage improvements.
/**
* Retrieve all recipients for a given email list using as little memory as
* possible (or at least less than the default Zend one which is known to hog a bit)
*
* !!! DO NOT USE Zend_Gdata_Gapps::retrieveAllRecipients !!!
*
* @param Zend_Gdata_Gapps $gapps Reference to Zend_Gdata_Gapps object
* @param array $members Reference to array of member email addresses
* @param string $emaiList The email list from which members should be retrieved
* @param string $startRecipient Get next page of recipients starting from this email
*/
function retrieveAllRecipients(&$gapps, &$members, $emailList, $startRecipient = null) {
// For memory comparison purposes
// echo '<br/>MEMORY: '.memory_get_usage();
// echo '<br/>MEMORY REAL: '.memory_get_usage(true);
// echo "<br/>$emailList + $startRecipient";
$feed = $gapps->retrievePageOfRecipients($emailList, $startRecipient);
foreach ($feed as $entry) {
// This is where our main memory improvement is compared to
// Zend_Gdata_Gapps::retrieveAllRecipients. We know we only need
// email address. No need to store the entire $entry object
$members[] = $entry->who->email;
}
$next = $feed->getLink('next');
// This is the other main memory improvement. Each feed object takes ~1MB
// of memory. Our listserv with 4500+ members would require 45 page calls
// and thus 45MB of memory using the Zend retrieveAllRecipients method.
// Unsetting our feed object before recursively calling our retrieveAllRecipients
// keeps memory usage relatively flat.
unset($feed);
if ($next !== null) {
// href will look like: https://apps-apis.google.com/a/feeds/ctsacentral.org/emailList/2.0/apisynctest/recipient/?startRecipient=test190%40example.com
if(strpos($next->href, 'startRecipient=')) {
$hrefParts = explode('startRecipient=', $next->href);
// Retrive all recipients starting with the email address test190%40example.com
retrieveAllRecipients(&$gapps, &$members, $emailList, $hrefParts[1]);
} else {
trigger_error('startRecipient not found. Could not get next page of recipients. There is a possiblity that the retrieveAllRecipients function did not return all recipients/members. You should investigate.', E_USER_ERROR);
}
}
}
@jonswaff
Copy link
Author

I needed to retrieve all email addresses belonging to a Google Group using the Provisioning API. My group had ~5000 members memory usage became an issue. This function is meant to be called instead of Zend_Gdata_Gapps::retrieveAllRecipients

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment