Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * @title Interface for Cisco Spark API V1
- * @author Ryan Thomas Huff <[email protected]>
- * @website http://ryanthomashuff.com
- * @description This is a basic API wrapper class that will demonstrate simple communication with the Cisco Spark API
- * @description for learning purposes. This wrapper does not have provision for OAuth and assumes the developer token
- *
- * <== DISCLAIMER ==>
- * Please feel free to do what you want with this code. The only thing I ask is that you
- * understand that by using this code, I make ABSOLUTELY NO EXPRESS OR IMPLIED WARRANTIES,
- * of any kind, for any reason, under any circumstance. By using or citing reference to this
- * code you take full responsibility of anything that may arise from such action.
- * <== DISCLAIMER ==>
- *
- */
- class sparkAPIV1 {
- //GLOBAL DATA
- var $SparkAccessToken = NULL;
- var $callBackUrl = NULL;
- var $roomId = NULL;
- //JSON URLs IN SPARK API
- //THESE ARE THE URL LOCATIONS THAT YOU CAN POST OR GET JSON DATA TO AND FROM
- var $roomsurl = 'https://api.ciscospark.com/v1/rooms';
- var $messagesurl = 'https://api.ciscospark.com/v1/messages';
- var $webhookurl = 'https://api.ciscospark.com/v1/webhooks';
- var $peopleurl = 'https://api.ciscospark.com/v1/people';
- var $membershipsurl = 'https://api.ciscospark.com/v1/memberships';
- /**
- * createWebHook| Used to create a WebHook within a room
- * @return string Returns the UUID of the WebHook that was created
- */
- function createWebHook()
- {
- $data = array(
- 'name' => 'MyCallback' . substr($roomId,-6),
- 'targetUrl' => $this->callBackUrl,
- 'resource' => 'messages',
- 'event' => 'created',
- 'filter' => 'roomId=' . $this->roomId
- );
- $options = array(
- 'http' => array(
- 'header' => "Authorization: Bearer $this->SparkAccessToken\r\nContent-type: application/json\r\n",
- 'method' => 'POST',
- 'content' => json_encode($data),
- ),
- );
- $context = stream_context_create($options);
- $result = json_decode(file_get_contents($this->webhookurl, false, $context));
- return $result->id;
- }
- /**
- * createRoomMessage AUsed to add a message into a room
- * @param string $message Used to contain the string to insert into the room as a message
- * @return null no returnable value
- */
- function createRoomMessage($message)
- {
- $data = array(
- 'roomId' => $this->roomId,
- 'text' => $message,
- );
- $options = array(
- 'http' => array(
- 'header' => "Authorization: Bearer $this->SparkAccessToken\r\nContent-type: application/json\r\n",
- 'method' => 'POST',
- 'content' => json_encode($data),
- ),
- );
- $context = stream_context_create($options);
- $result = json_decode(file_get_contents($this->messagesurl, false, $context));
- return;
- }
- /**
- * getRoomMessages Used to retrieve $max number of messages in a room
- * @param numeric $max Used to control the number of messages retrieved from the room
- * @return array An array of messages within the room
- */
- function getRoomMessages($max=10)
- {
- $data = array(
- 'roomId' => $this->roomId,
- 'max' => $max,
- );
- $options = array(
- 'http' => array(
- 'header' => "Authorization: Bearer $this->SparkAccessToken\r\nContent-type: application/json\r\n",
- 'method' => 'GET'
- ),
- );
- $context = stream_context_create($options);
- $result = json_decode(file_get_contents($this->messagesurl . '?' . http_build_query($data), false, $context));
- foreach ($result as $item)
- {
- foreach ($item as $key=>$value)
- {
- $dataSet[] = $value->text;
- }
- }
- return $dataSet;
- }
- }
- //EXAMPLE USAGE
- $sparkClass = new sparkAPIV1();
- $sparkClass->SparkAccessToken = 'MySparkUserAccessToken'; //YOUR SPARK USER'S ACCESS TOKEN
- $sparkClass->callBackUrl = 'MyCallBackURL'; //URL YOU WANT THE JSON CALLBACK DATA TO GO TO
- $sparkClass->roomId = 'MySparkRoomId'; //UUID OF SPARK ROOM
- echo 'Webhook ID: ' . $sparkClass->createWebHook(); //CREATES A WEBHOOK FOR THE USER, IN THE ROOM SPECIFIED BY THE ROOM ID
- $sparkClass->createRoomMessage('HELLO WORLD!'); //ISSUES A JSON POST TO THE ROOM WITH THE TEXT "HELLO WORLD!"
- var_dump($sparkClass->getRoomMessages(15)); //RETRIEVES ALL THE ROOM MESSAGES SPECIFIED (10 BY DEFAULT)
- ?>
Advertisement
Add Comment
Please, Sign In to add comment