Skip to content

Instantly share code, notes, and snippets.

@rgriffith
Created May 1, 2013 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgriffith/a3a2f86a3d2d5b5b1f57 to your computer and use it in GitHub Desktop.
Save rgriffith/a3a2f86a3d2d5b5b1f57 to your computer and use it in GitHub Desktop.
CollegiateLink API sample script. - CLStudentOrgs.class.php - contains the core class to pull from CollegiateLink's API using cURL. Fill in the empty API constants at the top, this information is obtained from CollegiateLink. - collegiateLinkStudentOrgs.php - this code queries CollegiateLink for organizations (in this case student orgs only) and…
<?php
// Temporarily set the charset to utf-8 because additional
// byte order characters are added to the response XML.
ini_set('default_charset', 'utf-8');
define('API_KEY', '');
define('SHARED_KEY', '');
define('SERVER_IP', $_SERVER['SERVER_ADDR']);
define('WS_URL', '');
class CLStudentOrgs
{
private $options = array('page_size'=>500);
private $clResponse = null;
public $curlUrl = '';
protected $actionUrls = array(
'organization/list',
'organization/roster',
'user/memberships',
'user/position',
'event/list'
);
function __construct() {
}
public function getResponse($actionUrl, array $opts = array()) {
if (!in_array($actionUrl, $this->actionUrls)) { return false; }
// Required parameters for the REST URL.
//
// Note: The time requires that it must be in-sync with an international time server,
// which Mustang is NOT (~4 minutes ahead). E.g. http://tycho.usno.navy.mil/cgi-bin/timer.pl
$params = array(
'apikey' => API_KEY,
'random' => str_replace('-','',$this->_uuid()),
'time' => sprintf("%.0f", ((time()-250)*1000))
);
$params['hash'] = md5(API_KEY.SERVER_IP.$params['time'].$params['random'].SHARED_KEY);
$params = array_merge($params, $opts);
// Build the parameters for the cURL URL.
$queryString = '';
foreach ($params as $key => $value) {
$queryString .= $key.'='.urlencode($value).'&';
}
$queryString = substr($queryString,0,-1);
$this->curlUrl = WS_URL.$actionUrl.'?'.$queryString;
// Get the organization list by making a cURL call to Collegiate Link.
$session = curl_init();
$curlOptions = array(
CURLOPT_URL => $this->curlUrl,
CURLOPT_TIMEOUT => 5,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 0,
CURLOPT_FAILONERROR => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0
);
curl_setopt_array($session, $curlOptions);
$response = curl_exec($session);
curl_close($session);
unset($session);
$this->clResponse = simplexml_load_string($response);
unset($response);
return $this->clResponse->results;
}
private function _uuid()
{
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(16384, 20479),
mt_rand(32768, 49151),
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 65535));
}
}
<?php
include_once('CLStudentOrgs.class.php');
$actionUrls = array(
'organization/list',
'organization/roster',
'user/memberships',
'user/position',
'event/list'
);
// Array to hold the master list of org categories.
$orgCategories = array();
// Array to hold all orgs separated by their category(ies)
// Note: orgs may be listed more than once.
$categorizedOrgs = array();
$clObj = new CLStudentOrgs();
$orgListXml = $clObj->getResponse($actionUrls[0]);
// Grab only orgs that are not a department or service.
$orgs = $orgListXml->xpath('//organization[type != "University Department" and type != "Service Organizations"]');
if (count($orgs) > 0) {
foreach ($orgs as $o) {
// Record some of the basic info for the org.
$orgsArr[(int)$o->id] = array(
'id' => (int)$o->id,
'name' => (string)$o->name,
'type' => (string)$o->type,
'status' => (string)$o->status,
'categories' => array(),
'description' => (string)$o->description,
'externalUrl' => (string)$o->externalWebsiteUrl,
'involvedUrl' => (string)$o->siteUrl,
'addresses' => array()
);
// Record the address(es) for the org.
if (count($o->addresses->address) > 0) {
foreach ($o->addresses->address as $a) {
if ((string)$a->city != '') {
$orgsArr[(int)$o->id]['addresses'][] = array(
'city' => (string)$a->city,
'country' => (string)$a->country,
'postalcode' => (string)$a->postalcode,
'state' => (string)$a->state,
'street1' => (string)$a->street1,
'street2' => (string)$a->street2,
'type' => (string)$a->type
);
}
}
}
// Record the roster for the org.
/*
// Uncomment below to pull rosters for each org...
$opts = array(
'id' => (int)$o->id,
'currentonly' => true
);
$rosterXml = $clObj->getResponse($actionUrls[1], $opts);
// If we have members, record them...
if ((int)$rosterXml->page->totalItems > 0) {
$roster = $rosterXml->xpath('//member');
foreach ($roster as $m) {
$orgsArr[(int)$o->id]['roster'][(int)$m->id] = array(
'id' => (int)$m->id,
'firstname' => (string)$m->firstname,
'lastname' => (string)$m->lastname,
'campusemail' => (string)$m->campusemail,
'positions' => array()
);
// Loop through the member's positions.
if (count($m->positions->position) > 0) {
foreach ($m->positions->position as $p) {
$orgsArr[(int)$o->id]['roster'][(int)$m->id]['positions'][] = array(
'name' => (string)$p->name,
'positionType' => (string)$p->positionType,
'positionTemplate' => (string)$p->positionTemplate,
'startdate' => (int) substr($p->startdate, 0, 10),
'enddate' => (int) substr($p->enddate, 0, 10)
);
}
}
}
}
*/
// Record the categor(ies) for the org.
if (count($o->categories->category) > 0) {
foreach ($o->categories->category as $c) {
$catName = trim((string)$c->name);
$catId = (int)$c->id;
if (!empty($catName)) {
$orgsArr[(int)$o->id]['categories'][] = array(
'id' => $catId,
'name' => $catName,
'ishidden' => (string)$c->ishidden,
);
// Record the category for our master listing...
if (!in_array($catName, $orgCategories) && $catName != 'University Departments') {
$orgCategories[$catId] = array(
'name' => $catName
);
}
$categorizedOrgs[$catName][(int)$o->id] = $orgsArr[(int)$o->id];
}
}
asort($orgCategories);
}
}
}
if (isset($_GET['dump'])) {
echo '<h1>All Orgs</h1><br />';
?><pre><?
var_dump($orgsArr);
?></pre><?
echo '<h1>All Categories</h1><br />';
?><pre><?
var_dump($orgCategories);
?></pre><?
echo '<h1>All Orgs by Category</h1><br />';
?><pre><?
var_dump($categorizedOrgs);
?></pre><?
} else {
//file_put_contents($_SERVER['DOCUMENT_ROOT'].'/lib/data/json/clorgs_allorgs.json', json_encode($orgsArr));
//file_put_contents($_SERVER['DOCUMENT_ROOT'].'/lib/data/json/clorgs_catorgs.json', json_encode($categorizedOrgs));
//file_put_contents($_SERVER['DOCUMENT_ROOT'].'/lib/data/json/clorgs_categories.json', json_encode($orgCategories));
}
die();
/*
DOCS
http://support.collegiatelink.net/entries/332558-web-services-developer-documentation
SAMPLES
https://studentlink.collegiatelink.net/ws/organization/list?
apikey=TestKey
random=c93cab70d4a14a5f9040cb7f499ec92
time=1289945845781
hash=b632b222c18b552fa7b9c79e5002270b
https://studentlink.collegiatelink.net/ws/organization/list?apikey=TestKey&random=c93cab70d4a14a5f9040cb7f499ec92&time=1289945845781&hash=b632b222c18b552fa7b9c79e5002270b
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment