Advertisement
Guest User

Prime API example

a guest
Jun 25th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $queryReachableAPs = array(
  2.     "reachabilityStatus" => "REACHABLE",
  3.     "unifiedApInfo.primaryMwar" => "WLC-Name",
  4.     "locationHierarchy" => "notStartsWith(Root)",
  5.     "unifiedApInfo.maintenanceMode" => "ne(TRUE)"
  6. );
  7. $queryReachableAPsJson = callPrimeAPI('AccessPointDetails.json?', $queryReachableAPs);
  8. if ($queryReachableAPsJson) {
  9.     $countReachableAPs = $queryReachableAPsJson->{'queryResponse'}->{'@count'};
  10.     if ($countReachableAPs > 0) {
  11.         $primeListHTML .= "<div class='list-group-item greenStyle d-flex align-items-center'><span>$upImage</span>$countReachableAPs APs Up</div>";
  12.     }
  13.     // Could do a debug here to see all avialable info (debug($queryReachableAPsJson);)
  14.    
  15. }
  16.  
  17. function debug($input)
  18. {
  19.  
  20.         #debug_print_backtrace();
  21.         if (is_string($input)) {
  22.             echo nl2br($input);
  23.         } else {
  24.             var_dump($input);
  25.         }
  26.    
  27. }
  28.  
  29. function callPrimeAPI($primeAPIResource, $queryArray)
  30. {
  31.     include ($_SERVER['DOCUMENT_ROOT'] . '/include/integeration.php');
  32.  
  33.     $primeURL = "";
  34.     if ($queryArray) {
  35.         // Web escape the URL
  36.         $primeWebAddress = "https://yourprime.yournet.net";
  37.     $primeAPIAddress = "$primeWebAddress/webacs/api/v4/data/";
  38.         $primeURL = $primeAPIAddress . $primeAPIResource . http_build_query($queryArray);
  39.     } else {
  40.         // If no query params are given, assign it's a direct resource URL
  41.         $primeURL = $primeAPIResource;
  42.     }
  43.  
  44.     $curl = curl_init();
  45.     curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  46.     $primeAuthData = "$primeAPIUsername:$primeAPIPassword";
  47.     curl_setopt($curl, CURLOPT_USERPWD, $primeAuthData);
  48.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  49.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  50.     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
  51.     curl_setopt($curl, CURLOPT_TIMEOUT, 3);
  52.  
  53.     $curlHeaders = [
  54.         "Accept: application/json"
  55.     ];
  56.  
  57.     curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
  58.     curl_setopt($curl, CURLOPT_URL, $primeURL);
  59.  
  60.     $primeResult = curl_exec($curl);
  61.  
  62.     $jsonResult = null;
  63.  
  64.     if (! $primeResult) {
  65.         debug("Error contacting Prime API");
  66.         return false;
  67.     } else {
  68.         $responseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
  69.         if ($responseCode === 200) {
  70.             $jsonResult = json_decode($primeResult);
  71.             return $jsonResult;
  72.         } else {
  73.             debug("Prime invalid response code. Code returned: $responseCode");
  74.             return false;
  75.         }
  76.     }
  77.     return false;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement