SHARE
TWEET

Interface for Cisco Spark API V1

ryanthomashuff Jan 18th, 2016 (edited) 568 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  *
  5.  * @title Interface for Cisco Spark API V1
  6.  * @author Ryan Thomas Huff <ryan@ryanthomashuff.com>
  7.  * @website http://ryanthomashuff.com
  8.  * @description This is a basic API wrapper class that will demonstrate simple communication with the Cisco Spark API
  9.  * @description for learning purposes. This wrapper does not have provision for OAuth and assumes the developer token
  10.  *
  11.  * <== DISCLAIMER ==>
  12.  * Please feel free to do what you want with this code. The only thing I ask is that you
  13.  * understand that by using this code, I make ABSOLUTELY NO EXPRESS OR IMPLIED WARRANTIES,
  14.  * of any kind, for any reason, under any circumstance. By using or citing reference to this
  15.  * code you take full responsibility of anything that may arise from such action.
  16.  * <== DISCLAIMER ==>
  17.  *
  18.  */
  19.  
  20. class sparkAPIV1 {
  21.    
  22.     //GLOBAL DATA
  23.     var $SparkAccessToken = NULL;
  24.     var $callBackUrl = NULL;
  25.     var $roomId = NULL;
  26.    
  27.     //JSON URLs IN SPARK API
  28.     //THESE ARE THE URL LOCATIONS THAT YOU CAN POST OR GET JSON DATA TO AND FROM
  29.     var $roomsurl = 'https://api.ciscospark.com/v1/rooms';
  30.     var $messagesurl = 'https://api.ciscospark.com/v1/messages';
  31.     var $webhookurl = 'https://api.ciscospark.com/v1/webhooks';
  32.     var $peopleurl = 'https://api.ciscospark.com/v1/people';
  33.     var $membershipsurl = 'https://api.ciscospark.com/v1/memberships';
  34.    
  35.  
  36.     /**
  37.      * createWebHook| Used to create a WebHook within a room
  38.      * @return string Returns the UUID of the WebHook that was created
  39.      */    
  40.     function createWebHook()
  41.     {
  42.         $data = array(
  43.             'name' => 'MyCallback' . substr($roomId,-6),
  44.             'targetUrl' => $this->callBackUrl,
  45.             'resource' => 'messages',
  46.             'event' => 'created',
  47.             'filter' => 'roomId=' . $this->roomId
  48.             );
  49.         $options = array(
  50.             'http' => array(
  51.                 'header'  => "Authorization: Bearer $this->SparkAccessToken\r\nContent-type: application/json\r\n",
  52.                 'method'  => 'POST',
  53.                 'content' => json_encode($data),
  54.             ),
  55.         );
  56.         $context  = stream_context_create($options);
  57.         $result = json_decode(file_get_contents($this->webhookurl, false, $context));
  58.         return $result->id;
  59.     }
  60.    
  61.     /**
  62.      * createRoomMessage AUsed to add a message into a room
  63.      * @param string $message Used to contain the string to insert into the room as a message
  64.      * @return null no returnable value
  65.      */
  66.     function createRoomMessage($message)
  67.     {
  68.         $data = array(
  69.             'roomId' => $this->roomId,
  70.             'text' => $message,            
  71.             );
  72.         $options = array(
  73.             'http' => array(
  74.                 'header'  => "Authorization: Bearer $this->SparkAccessToken\r\nContent-type: application/json\r\n",
  75.                 'method'  => 'POST',
  76.                 'content' => json_encode($data),
  77.             ),
  78.         );
  79.         $context  = stream_context_create($options);
  80.         $result = json_decode(file_get_contents($this->messagesurl, false, $context));
  81.         return;
  82.     }
  83.    
  84.     /**
  85.      * getRoomMessages Used to retrieve $max number of messages in a room
  86.      * @param numeric $max Used to control the number of messages retrieved from the room
  87.      * @return array An array of messages within the room
  88.      */
  89.     function getRoomMessages($max=10)
  90.     {
  91.         $data = array(
  92.             'roomId' => $this->roomId,
  93.             'max' => $max,
  94.             );
  95.         $options = array(
  96.             'http' => array(
  97.                 'header'  => "Authorization: Bearer $this->SparkAccessToken\r\nContent-type: application/json\r\n",
  98.                 'method'  => 'GET'
  99.             ),
  100.         );
  101.         $context  = stream_context_create($options);
  102.         $result = json_decode(file_get_contents($this->messagesurl . '?' . http_build_query($data), false, $context));
  103.         foreach ($result as $item)
  104.         {
  105.             foreach ($item as $key=>$value)
  106.             {
  107.                 $dataSet[] = $value->text;
  108.             }
  109.         }
  110.         return $dataSet;
  111.     }
  112. }
  113.  
  114. //EXAMPLE USAGE
  115.  
  116. $sparkClass = new sparkAPIV1();
  117. $sparkClass->SparkAccessToken = 'MySparkUserAccessToken';  //YOUR SPARK USER'S ACCESS TOKEN
  118. $sparkClass->callBackUrl = 'MyCallBackURL';  //URL YOU WANT THE JSON CALLBACK DATA TO GO TO
  119. $sparkClass->roomId = 'MySparkRoomId';  //UUID OF SPARK ROOM
  120. echo 'Webhook ID: ' . $sparkClass->createWebHook();  //CREATES A WEBHOOK FOR THE USER, IN THE ROOM SPECIFIED BY THE ROOM ID
  121. $sparkClass->createRoomMessage('HELLO WORLD!');  //ISSUES A JSON POST TO THE ROOM WITH THE TEXT "HELLO WORLD!"
  122. var_dump($sparkClass->getRoomMessages(15)); //RETRIEVES ALL THE ROOM MESSAGES SPECIFIED (10 BY DEFAULT)
  123.  
  124. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top