Skip to content

Instantly share code, notes, and snippets.

@jmertic
Last active December 21, 2015 23:49
Show Gist options
  • Save jmertic/6385293 to your computer and use it in GitHub Desktop.
Save jmertic/6385293 to your computer and use it in GitHub Desktop.
Example of how to get a user's timezone information, if you are an admin user
<?php
// specify the REST web service to interact with
$url = 'http://sugarinstance/rest/v10';
// And admin username/password
$admin_username = '<<admin username>>';
$admin_password = '<<admin password>>';
// And username you whom you want to find out thier timezone
$other_user = '<<user you want to sudo to>>'
/**
* Authenicate and get back token
*/
$curl = curl_init($url . "/oauth2/token");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the POST arguments to pass to the Sugar server
$rawPOSTdata = array(
"grant_type" => "password",
"username" => $admin_username,
"password" => $admin_password,
"client_id" => "sugar",
"client_secret" => "",
"platform" => "base",
);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($rawPOSTdata));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// Make the REST call, returning the result
$response = curl_exec($curl);
if (!$response) {
die("Connection Failure.\n");
}
// Convert the result from JSON format to a PHP array
$result = json_decode($response);
curl_close($curl);
$token = $result->access_token;
/**
* Now, let's sudo to the other whom we want to find out thier timezone
*/
// Open a curl session for making the call
$curl = curl_init($url . "/oauth2/sudo/{$other_user}");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"OAuth-Token: $token"));
curl_setopt($curl, CURLOPT_POSTFIELDS, '');
// Make the REST call, returning the result
$response = curl_exec($curl);
if (!$response) {
die("Connection Failure.\n");
}
// Convert the result from JSON format to a PHP array
$result = json_decode($response);
curl_close($curl);
$otherUserAccessToken = $result->access_token;
/**
* Subsequent call to get my name
*/
// Open a curl session for making the call
$curl = curl_init($url . "/me/preference/timezone");
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"OAuth-Token: $otherUserAccessToken"));
// Make the REST call, returning the result
$response = curl_exec($curl);
if (!$response) {
die("Connection Failure.\n");
}
// Convert the result from JSON format to a PHP array
$timezone = json_decode($response);
curl_close($curl);
echo "Timezone for {$other_user} is {$timezone}\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment