*/
$nameInstance = new Google_Service_Directory_UserName();
$nameInstance->setGivenName('John');
$nameInstance->setFamilyName('Doe');
$email = '*****@*****.**';
$password = '******';
$userInstance = new Google_Service_Directory_User();
$userInstance->setName($nameInstance);
$userInstance->setHashFunction("MD5");
$userInstance->setPrimaryEmail($email);
$userInstance->setPassword(hash("md5", $password));
try {
    $createUserResult = $service->users->insert($userInstance);
    var_dump($createUserResult);
} catch (Google_IO_Exception $gioe) {
    echo "Error in connection: " . $gioe->getMessage();
} catch (Google_Service_Exception $gse) {
    echo "User already exists: " . $gse->getMessage();
}
/**
 * If you want it, add the user to a group
 */
$memberInstance = new Google_Service_Directory_Member();
$memberInstance->setEmail($email);
$memberInstance->setRole('MEMBER');
$memberInstance->setType('USER');
try {
    $insertMembersResult = $service->members->insert('*****@*****.**', $memberInstance);
} catch (Google_IO_Exception $gioe) {
    echo "Error in connection: " . $gioe->getMessage();
}
 function membersmaintain($members)
 {
     global $success;
     $return = $this->service->members->listMembers($this->groupfullname);
     //                $gmembers = get_group_permissions($this->groupname, $this->domain, $this->authstring);
     $gmembers = array();
     foreach ($return as $key => $value) {
         $gmembers[$value->email] = strtolower($value->role);
     }
     // check differences
     $deleted = array_diff_assoc($gmembers, $members);
     $added = sizeof($members) > 200 ? array() : array_diff_assoc($members, $gmembers);
     /************************************************
         To actually make the batch call we need to 
         enable batching on the client - this will apply 
         globally until we set it to false. This causes
         call to the service methods to return the query
         rather than immediately executing.
        ************************************************/
     $this->client->setUseBatch(true);
     $batch = new Google_Http_Batch($this->client);
     // delete first due to aliases
     foreach ($deleted as $email => $role) {
         $return = $this->service->members->delete($this->groupfullname, $email);
         $batch->add($return, $emailAddress);
         if ($success == null) {
             $this->log("{$member} delete SUCCESS", $this->courseid, null, $permission . ':' . s($response->response));
         } else {
             $this->log("{$member} delete FAILED", $this->courseid, null, $permission . ':');
         }
     }
     // then add
     foreach ($added as $email => $role) {
         $enrollee = new Google_Service_Directory_Member();
         $enrollee->setEmail($email);
         $enrollee->setRole(strtoupper($role));
         $return = $this->service->members->insert($this->groupfullname, $enrollee);
         $batch->add($return, $emailAddress);
         if ($success == null) {
             $this->log("{$member} add SUCCESS", $this->courseid, null, $permission . ':' . s($response->response));
         } else {
             $this->log("{$member} ADD FAILED ", $this->courseid, null, $permission . ':');
         }
     }
     /************************************************
         Executing the batch will send all requests off
         at once.
        ************************************************/
     $results = $batch->execute();
     $this->client->setUseBatch(false);
 }