├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src └── UptimeRobot └── API.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Cory 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHP Wrapper For UptimeRobot.com 2 | ============== 3 | 4 | This is a basic PHP wrapper for https://uptimerobot.com/api 5 | 6 | ## Prerequisites 7 | * Configure the $config apiKey 8 | * Must be running PHP >= 5.4 9 | * Format will be JSON & there will be no JSONCallback 10 | 11 | ## Composer 12 | Add this to your composer.json 13 | 14 | ```JSON 15 | { 16 | "require": { 17 | "ckdarby/php-uptimerobot": "@stable" 18 | } 19 | } 20 | ``` 21 | 22 | ## Example 23 | 24 | ```PHP 25 | 'APIKEY', 34 | 'url' => 'https://api.uptimerobot.com' 35 | ]; 36 | 37 | try { 38 | 39 | //Initalizes API with config options 40 | $api = new API($config); 41 | 42 | //Define parameters for our getMethod request 43 | $args = [ 44 | 'showTimezone' => 1 45 | ]; 46 | 47 | //Makes request to the getMonitor Method 48 | $results = $api->request('/getMonitors', $args); 49 | 50 | //Output json_decoded contents 51 | var_dump($results); 52 | 53 | } catch (Exception $e) { 54 | echo $e->getMessage(); 55 | //Output various debug information 56 | var_dump($api->debug); 57 | } 58 | 59 | ``` 60 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ckdarby/php-uptimerobot", 3 | "description": "PHP Wrapper for UptimeRobot.com API", 4 | "homepage": "https://github.com/ckdarby/PHP-UptimeRobot/", 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Cory Kennedy-Darby", 10 | "email": "ckdarby+uptimerobotapi@gmail.com", 11 | "homepage": "https://github.com/ckdarby/" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.4.0", 16 | "ext-curl": "*" 17 | }, 18 | "autoload": { 19 | "psr-0": { 20 | "UptimeRobot": "src/" 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/UptimeRobot/API.php: -------------------------------------------------------------------------------- 1 | args['apiKey'] = $config['apiKey']; 37 | $this->args['format'] = 'json'; 38 | $this->args['noJsonCallback'] = 1; 39 | 40 | $this->url = $config['url']; 41 | 42 | // Set options for curl 43 | $this->options = $this->getOptions($options); 44 | } 45 | 46 | /** 47 | * Makes curl call to the url & returns output. 48 | * 49 | * @param string $resource The resource of the api 50 | * @param array $args Array of options for the query query 51 | * 52 | * @return array json_decoded contents 53 | * @throws \Exception If the curl request fails 54 | */ 55 | public function request($resource, $args = array()) 56 | { 57 | 58 | $url = $this->buildUrl($resource, $args); 59 | $curl = curl_init($url); 60 | 61 | curl_setopt_array($curl, $this->options); 62 | $this->contents = curl_exec($curl); 63 | $this->setDebug($curl); 64 | 65 | if (curl_errno($curl) > 0) { 66 | throw new \Exception('There was an error while making the request'); 67 | } 68 | 69 | $jsonDecodeContent = json_decode($this->contents, true); 70 | 71 | if(is_null($jsonDecodeContent)) { 72 | throw new \Exception('Unable to decode JSON response'); 73 | } 74 | 75 | return $jsonDecodeContent; 76 | } 77 | 78 | /** 79 | * Get options for curl. 80 | * 81 | * @param $options 82 | * 83 | * @return array 84 | */ 85 | private function getOptions($options) 86 | { 87 | $conf = [ 88 | CURLOPT_HEADER => false, 89 | CURLOPT_RETURNTRANSFER => true, 90 | CURLOPT_SSL_VERIFYPEER => false, 91 | CURLOPT_FOLLOWLOCATION => true 92 | ]; 93 | 94 | if (isset($options['timeout'])) { 95 | $conf[CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000; 96 | } 97 | if (isset($options['connect_timeout'])) { 98 | $conf[CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000; 99 | } 100 | 101 | return $conf; 102 | } 103 | 104 | /** 105 | * Builds the url for the curl request. 106 | * 107 | * @param string $resource The resource of the api 108 | * @param array $args Array of options for the query query 109 | * 110 | * @return string Finalized Url 111 | */ 112 | private function buildUrl($resource, $args) 113 | { 114 | //Merge args(apiKey, Format, noJsonCallback) 115 | $args = array_merge($args, $this->args); 116 | $query = http_build_query($args); 117 | 118 | $url = $this->url; 119 | $url .= $resource . '?' . $query; 120 | 121 | return $url; 122 | } 123 | 124 | /** 125 | * Sets debug information from last curl. 126 | * 127 | * @param resource $curl Curl handle 128 | */ 129 | private function setDebug($curl) 130 | { 131 | $this->debug = [ 132 | 'errorNum' => curl_errno($curl), 133 | 'error' => curl_error($curl), 134 | 'info' => curl_getinfo($curl), 135 | 'raw' => $this->contents, 136 | ]; 137 | } 138 | } 139 | --------------------------------------------------------------------------------