Advertisement
Guest User

CampusLabs API Demo

a guest
Jun 14th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.97 KB | None | 0 0
  1. <?php
  2. /* DataSync.php - Version 2.0
  3.  * Created By: Colin Knapp - SA Technology and Web Coordinator
  4.  * Purpose: A direct interface to the CampusLabs WebServices API (CLWSAPI)
  5.  *        : This will not only request and retrieve data with parameters given
  6.  *        : But will also parse the results and populate a database online
  7.  */
  8. require_once ("/www_vol/data/sa-data/ccc-api/scripts/connect/mysqli.connect.php");
  9.  
  10. ini_set('output_buffering', 0);
  11. ini_set('implicit_flush', 1);
  12.  
  13. header("Content-Type: text/html; charset=utf-8");
  14.  
  15. if (isset($_GET)) {
  16.     $instruction = "";
  17.     $parameters = "";
  18.  
  19.     if (isset($_GET['instruction'])) {
  20.         $instruction = $_GET['instruction'];
  21.     }
  22.  
  23.     if (isset($_GET['parameters'])) {
  24.         $parameters = $_GET['parameters'];
  25.     }
  26.  
  27.     $dataClass = new dataSync($instruction, $parameters);
  28.     if (isset($dataClass -> result)) {
  29.         //throw new Exception(var_dump($dataClass -> result));
  30.         echo $dataClass -> result;
  31.     }
  32. } else {
  33.     echo "No GET parameters sent. (DataSync.php)";
  34. }
  35.  
  36. class dataSync {
  37.     // Constants
  38.     private $apiKeyConst = "YOUR API KEY - The one from CL like ABC12SAGHEHWAA"
  39.     private $apiUserConst = "YOUR API USER - Like User-01"
  40.     private $ipAddrConst = "YOUR IP GIVEN TO CL"
  41.     private $lastMessage = "";
  42.     private $messageArray = array();
  43.     private $messageCnt = 0;
  44.     private $totalOrgs = 0;
  45.     private $totalUsers = 0;
  46.  
  47.     public $result = null;
  48.  
  49.     // Initializing Method
  50.     public function __construct($i, $p){
  51.         if($i == ""){
  52.             return FALSE;
  53.         } else {
  54.             if ($p != "") {
  55.                 //throw new Exception(var_dump(json_decode($p)));  
  56.                 $params = json_decode(stripslashes(urldecode($p)), true);  
  57.                 //throw new Exception(var_dump($params));      
  58.             }
  59.             $this -> callFunction($i, $params);
  60.         }
  61.     }
  62.    
  63.     public function callFunction($inst, $params = NULL){
  64.         if($params == NULL){
  65.             call_user_func(array('self',$inst));
  66.         } else {
  67.             call_user_func(array('self',$inst), $params);
  68.         }
  69.     }
  70.    
  71.     public function fullescape($str) {
  72.         # convert characters > 255 into HTML entities
  73.         $convmap = array(0xFF, 0x2FFFF, 0, 0xFFFF);
  74.         $str = mb_encode_numericentity($str, $convmap, "UTF-8");
  75.  
  76.         # escape HTML entities, so they are not urlencoded
  77.         $str = preg_replace('/&#([0-9a-fA-F]{2,5});/i', 'mark\\1mark', $str);
  78.         $str = urlencode($str);
  79.  
  80.         # now convert escaped entities into unicode url syntax
  81.         $str = preg_replace('/mark([0-9a-fA-F]{2,5})mark/i', '%u\\1', $str);
  82.  
  83.         $str = str_replace("+", "%20", $str);
  84.  
  85.         return $str;
  86.     }
  87.  
  88.     private function getGUID() {
  89.         // Get a MS style GUID since PHP uses UUIDs
  90.         // GUID Format = EC3597BD-A1DE-B544-9F97-8611944356CC
  91.  
  92.         if (function_exists('com_create_guid')) {
  93.             return com_create_guid();
  94.         } else {
  95.             mt_srand((double)microtime() * 10000);
  96.             $charid = strtoupper(md5(uniqid(rand(), TRUE)));
  97.             $hyphen = chr(45);
  98.             $uuid = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12);
  99.  
  100.             return $uuid;
  101.         }
  102.     }
  103.    
  104.     private function buildHash() {
  105.         $guid = $this -> getGUID();
  106.         $utc = (string)round(microtime(true) * 1000);
  107.  
  108.         $prehash = $this -> apiUserConst . $this -> ipAddrConst . $utc . $guid . $this -> apiKeyConst;
  109.         $hash = (string)md5($prehash);
  110.         $parameters = array();
  111.  
  112.         // Required Parameters
  113.         $parameters['apikey'] = $this -> apiUserConst;
  114.         $parameters['time'] = $utc;
  115.         $parameters['random'] = $guid;
  116.         $parameters['hash'] = $hash;
  117.  
  118.         // Optional Parameters
  119.         //$parameters['pagesize'] = 500;
  120.         // Max results
  121.  
  122.         return $parameters;
  123.     }
  124.    
  125.     private function fetchData($url, $parameters){
  126.         $requesturl = $url;
  127.         $cnt        = 0;
  128.        
  129.         foreach($parameters as $key => $value){
  130.             $op   = ($cnt == 0 ? "?" : "&");
  131.             $url .= $op . $key . "=" . urlencode($value);
  132.             $cnt++;
  133.         }
  134.        
  135.         //echo $url . "\r\n";
  136.        
  137.         $session = curl_init($url);
  138.         curl_setopt($session, CURLOPT_HEADER, false);
  139.         curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
  140.        
  141.         $response = curl_exec($session);
  142.         return $response;
  143.     }
  144.    
  145.     private function getRosters(){
  146.         $time = time();
  147.        
  148.         ob_end_flush();
  149.         ob_start();
  150.         $page                   = 1;
  151.         $parameters             = $this -> buildHash();
  152.         $parameters['id']       = 64382;
  153.        
  154.         $parameters['pagesize'] = 500;
  155.        
  156.         $url        = "https://your.url/ws/organization/roster";
  157.         $xmlData    = simplexml_load_string($this -> fetchData($url, $parameters));
  158.        
  159.         $decoded    = $this -> xml2array($xmlData);
  160.         $roster     = $this -> processArray($decoded);
  161.        
  162.         print "<pre>";
  163.         print var_dump($roster);
  164.         print "</pre>";    
  165.     }
  166.  
  167.     private function xml2array(SimpleXMLElement $parent) {
  168.         $array = array();
  169.  
  170.         foreach ($parent as $name => $element) {
  171.             ($node = & $array[$name]) && (1 === count($node) ? $node = array($node) : 1) && $node = & $node[];
  172.    
  173.             $node = $element->count() ? $this -> xml2array($element) : trim($element);
  174.         }
  175.    
  176.         return $array;
  177.     }
  178.    
  179.     private function processArray($in){
  180.         $pageData       = $in['results']['page'];
  181.         $itemData       = $in['results']['page']['items'];
  182.         $returnData         = NULL;
  183.        
  184.         foreach($itemData as $key => $val){
  185.             $holder = $itemData[$key];         
  186.            
  187.             switch($key){
  188.                 case "event":
  189.                    
  190.                 break;                 
  191.                    
  192.                 case "member":
  193.                     $curmem     = array();
  194.                     $moc        = 0;
  195.                     $start      = -1;
  196.                     $reccnt     = intval($pageData['pageSize']) - 1;                   
  197.                                    
  198.                     for($i=$start;$i<$reccnt;$i++){                
  199.                         if($i == -1){                          
  200.                             $memrecord  = $holder;
  201.                         } else {
  202.                             $memrecord  = $holder[$i];                             
  203.                         }
  204.                        
  205.                         $pcnt                       = 0;
  206.                         $positions                  = $memrecord['positions']['position'];                                             
  207.                         $curmem[$moc]               = array();         
  208.                         $curmem[$moc]['position']   = array();
  209.                         $curmem[$moc]['position'][$pcnt] = array();
  210.                        
  211.                         foreach($positions as $pk => $pv){                                                     
  212.                             if(!is_numeric($pk)){                              
  213.                                 $curmem[$moc]['position'][$pcnt]['pos_' . $pk] = $pv;
  214.                             } else {                               
  215.                                 $pos = $positions[$pk];
  216.                                 $pcnt++;
  217.                                
  218.                                 foreach($pos as $ppk => $ppv){
  219.                                     $curmem[$moc]['position'][$pcnt]['pos_' . $ppk] = $ppv;
  220.                                 }                                                          
  221.                             }                          
  222.                         }
  223.                        
  224.                         foreach($memrecord as $hk => $hv){
  225.                             if(!is_numeric($hk) && $hk != "positions"){
  226.                                 $curmem[$moc][$hk] = $hv;                              
  227.                             }
  228.                         }
  229.                        
  230.                         $moc++;
  231.                     }                                      
  232.                
  233.                     return $curmem;
  234.                 break;
  235.                    
  236.                 case "membership":
  237.                    
  238.                 break;
  239.                
  240.                 case "organization":
  241.                     $curorg     = array();
  242.                     $coc        = 0;
  243.                     $start      = -1;
  244.                     $reccnt     = intval($pageData['totalItems']) - 1;                 
  245.                                    
  246.                     for($i=$start;$i<$reccnt;$i++){                
  247.                         if($i == -1){                          
  248.                             $orgrecord  = $holder;
  249.                         } else {
  250.                             $orgrecord  = $holder[$i];                                             
  251.                         }
  252.                        
  253.                         $address    = $orgrecord['addresses']['address'];
  254.                         $category   = $orgrecord['categories']['category'];
  255.                         $customfld  = $orgrecord['customfields']['customfield'];   
  256.                         array_splice($orgrecord, 0, 3);    
  257.                        
  258.                         $getcf      = false;
  259.                         $year       = "";
  260.                        
  261.                         foreach($customfld as $kcf => $vcf){
  262.                             if($kcf == "name"){
  263.                                 if($vcf == "Year Founded:"){
  264.                                     $getcf = true;
  265.                                 }
  266.                             }
  267.                                
  268.                             if($getcf && $kcf == "values"){
  269.                                 $year = $customfld[$kcf]["string"];
  270.                                 break;
  271.                             }
  272.                         }
  273.                                                
  274.                         $curorg[$coc] = array();
  275.                        
  276.                         foreach($address as $ak => $av){
  277.                             $curorg[$coc]['adr_' . $ak] = $av;
  278.                         }
  279.                        
  280.                         foreach($category as $ck => $cv){
  281.                             $curorg[$coc]['cat_' . $ck] = $cv;
  282.                         }
  283.                        
  284.                         foreach($orgrecord as $hk => $hv){
  285.                             if(!is_numeric($hk)){
  286.                                 if($hk == "description"){
  287.                                     $hv = htmlspecialchars($hv);
  288.                                 }
  289.                                 $curorg[$coc][$hk] = $hv;                              
  290.                             }
  291.                            
  292.                         }
  293.                        
  294.                         if($year != ""){
  295.                             $curorg[$coc]['year'] = $year;
  296.                         }
  297.                         $coc++;
  298.                     }                                      
  299.                
  300.                     return $curorg;    
  301.                 break;                 
  302.                  
  303.             }
  304.         }  
  305.     }
  306. }
  307.  
  308. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement