Skip to content

Instantly share code, notes, and snippets.

@pitbulk
Last active March 15, 2017 21:31
Show Gist options
  • Save pitbulk/88e495a7c2a909864876ce110e61ae5a to your computer and use it in GitHub Desktop.
Save pitbulk/88e495a7c2a909864876ce110e61ae5a to your computer and use it in GitHub Desktop.
Onelogin - Groups APi
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/groups";
$access_token = "<access_token>";
// Parameters
$query_parameters = array(
// -- Search --
'id' => '',
// -- Pagination --
// 'after_cursor' => '',
// 'before_cursor' => '',
// -- Limit --
'limit' => 50, // Max limit (default value)
// -- Sort --
// 'sort' => '+id',
);
$query = http_build_query(array_filter($query_parameters));
$authorization = "bearer:$access_token";
if (!empty($query)) {
$url .= '?' . $query;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
} else if (empty($result_data->data)) {
// No result
$groups = array();
} else {
$groups = $result_data->data;
$before_cursor = $result_data->pagination->before_cursor;
$after_cursor = $result_data->pagination->after_cursor;
// Now if $after_cursor is not empty, I can pass it as
// a parameter of $query_parameters and execute again
// the curl to retrieve all groups.
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/groups";
$access_token = "<access_token>";
// Parameters
$id = "<group_id>";
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
$errorMsg .= " || ". $result_data->status->message;
throw new Exception($errorMsg);
} else {
$group = $result_data->data;
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment