Skip to content

Instantly share code, notes, and snippets.

@mshmsh5000
Last active October 8, 2015 15:54
Show Gist options
  • Save mshmsh5000/1f011e2073c05ba00652 to your computer and use it in GitHub Desktop.
Save mshmsh5000/1f011e2073c05ba00652 to your computer and use it in GitHub Desktop.
function dosomething_northstar_get_northstar_user($id)
<?php
/**
* Get user data from Northstar.
*
* @param int $id
* Northstar user ID
*
* @return object
*/
function dosomething_northstar_get_northstar_user($id) {
$northstar_user_info = cache_get('northstar_user_info_' . $id, 'cache_northstar_user_info');
if (empty($northstar_user_info)) {
$client = _dosomething_northstar_build_http_client();
$northstar_user_info = drupal_http_request($client['base_url'] . '/users/drupal_id/' . $id, array(
'headers' => $client['headers'],
'method' => 'GET',
));
// Don't cache error responses.
if (!empty($northstar_user_info->error)) {
$error = sprintf("Error fetching Northstar user data, uid=%d: '%s'", $id, $northstar_user_info->error);
watchdog_exception('northstar', new Exception($error));
} else {
cache_set('northstar_user_info_' . $id, $northstar_user_info, 'cache_northstar_user_info', REQUEST_TIME + 60*60*24);
}
}
return $northstar_user_info;
}
@weerd
Copy link

weerd commented Oct 8, 2015

Good stuff! I had no idea sprintf() was a thing in PHP. Recently learned about printf() in bash, but didn't realize PHP had its own :)

Learningz 📖

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