Something similar to this will add users “bill” and “ben” to group “flower_pot_men”:
/**
* Add users to a specified group, create the group if it doesn't exist.
*
* @param string $group_name name of group
* @param array $usernames users to add to group
*
* @return mixed HTTP return code and API return object
*/
public function add_users_to_group( $group_name, $usernames = array() ) {
$group_id = null;
$result = $this->get_request( "/admin/groups.json" );
if ( $result->http_code != 200 ) {
return $result;
}
foreach ( $result->result as $group ) {
if ( $group->name === $group_name ) {
$group_id = $group->id;
break;
}
$group_id = false;
}
if ( ! $group_id ) {
$result = $this->post_request( '/admin/groups', array( 'name' => $group_name ) );
if ( $result->http_code != 200 ) {
return $result;
}
$group_id = $result->result->basic_group->id;
}
if ( ! empty( $usernames ) ) {
$params = array(
'usernames' => implode( ',', $usernames )
);
$result = $this->put_request( '/admin/groups/' . $group_id . '/members.json', $params );
}
return $result;
}
$group_name = 'flower_pot_men';
$usernames = array( 'bill', 'ben' );
$result = $api->add_users_to_group( $group_name, $usernames );
if ( $result->http_code != 200 ) {
// @todo handle the error
}
EDIT: The key point here is the /admin part is different from yours.
EDIT #2: Updated to correctly reflect ID usage.