├── LICENSE ├── README.md └── Slack.php /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 by Low Yong Zhen 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Slack API 2 | ========= 3 | 4 | A super simple wrapper for Slack API 5 | 6 | Requires > PHP 5. 7 | 8 | Installation 9 | ------------ 10 | 11 | Download and include the php file. 12 | 13 | Usage 14 | ----- 15 | 16 | > See more methods on the [Slack API documentation](https://api.slack.com/methods). 17 | 18 | Initializing 19 | Requires a legacy token, read more about it [here](https://api.slack.com/custom-integrations/legacy-tokens). 20 | 21 | ```php 22 | $Slack = new Slack('xoxp-your-legacy-token'); 23 | ``` 24 | 25 | List users 26 | 27 | ```php 28 | $Slack = new Slack('xoxp-your-legacy-token'); 29 | print_r($Slack->call('users.list')); 30 | ``` 31 | 32 | Invite user to a channel 33 | 34 | ```php 35 | $Slack = new Slack('xoxp-your-legacy-token'); 36 | print_r($Slack->call('channels.invite', array( 37 | 'channel' => 'C1234567890', 38 | 'user' => 'U1234567890' 39 | ))); 40 | ``` 41 | 42 | Author 43 | ------- 44 | 45 | - [Low Yong Zhen](mailto:yz@stargate.io) 46 | 47 | License 48 | ------- 49 | 50 | Copyright (c) 2014 51 | 52 | Licensed under the [MIT License](http://yzlow.mit-license.org/). 53 | -------------------------------------------------------------------------------- /Slack.php: -------------------------------------------------------------------------------- 1 | 10 | * @version 1.0.0 11 | */ 12 | class Slack { 13 | 14 | private $api_token; 15 | private $api_endpoint = 'https://slack.com/api/'; 16 | 17 | /** 18 | * Create a new instance 19 | * @param string $api_token Your Slack api bearer token 20 | */ 21 | function __construct($api_token){ 22 | $this->api_token = $api_token; 23 | } 24 | 25 | /** 26 | * Calls an API method. You don't have to pass in the token, it will automatically be included. 27 | * @param string $method The API method to call. 28 | * @param array $args An associative array of arguments to pass to the API. 29 | * @param integer $timeout Set maximum time the request is allowed to take, in seconds. 30 | * @return array The response as an associative array, JSON-decoded. 31 | */ 32 | public function call($method, $args = array(), $timeout = 10){ 33 | return $this->request($method, $args, $timeout); 34 | } 35 | 36 | /** 37 | * Performs the underlying HTTP request. 38 | * @param string $method The API method to call. 39 | * @param array $args An associative array of arguments to pass to the API. 40 | * @param integer $timeout Set maximum time the request is allowed to take, in seconds. 41 | * @return array The response as an associative array, JSON-decoded. 42 | */ 43 | private function request($method, $args = array(), $timeout = 10){ 44 | $url = str_replace('', $method, $this->api_endpoint); 45 | $args['token'] = $this->api_token; 46 | 47 | if (function_exists('curl_version')){ 48 | $ch = curl_init(); 49 | curl_setopt($ch, CURLOPT_URL, $url); 50 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 51 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 52 | curl_setopt($ch, CURLOPT_POST, true); 53 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 54 | curl_setopt($ch, CURLOPT_POSTFIELDS, $args); 55 | $result = curl_exec($ch); 56 | curl_close($ch); 57 | } else { 58 | $post_data = http_build_query($args); 59 | $result = file_get_contents($url, false, stream_context_create(array( 60 | 'http' => array( 61 | 'protocol_version' => 1.1, 62 | 'method' => 'POST', 63 | 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . 64 | "Content-length: " . strlen($post_data) . "\r\n" . 65 | "Connection: close\r\n", 66 | 'content' => $post_data 67 | ), 68 | ))); 69 | } 70 | 71 | return $result ? json_decode($result, true) : false; 72 | } 73 | } --------------------------------------------------------------------------------