├── .travis.yml ├── LICENSE.md ├── README.md ├── _config.yml ├── composer.json ├── phpunit.xml ├── src ├── Connection.php └── Rant.php └── tests └── MainTest.php /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - '5.6' 4 | - '7.0' 5 | - '7.1' 6 | 7 | before_install: 8 | - composer self-update 9 | 10 | install: 11 | - composer install --no-dev --ignore-platform-reqs 12 | 13 | script: phpunit 14 | 15 | cache: 16 | directories: 17 | vendor 18 | 19 | before_cache: 20 | - composer update 21 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Owen Voke 4 | 5 | >Permission is hereby granted, free of charge, to any person obtaining a copy 6 | >of this software and associated documentation files (the "Software"), to deal 7 | >in the Software without restriction, including without limitation the rights 8 | >to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | >copies of the Software, and to permit persons to whom the Software is 10 | >furnished to do so, subject to the following conditions: 11 | > 12 | >The above copyright notice and this permission notice shall be included in 13 | >all 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, 17 | >FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | >AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | >LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | >OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | >THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # devrant-php 2 | 3 | [![Build Status](https://travis-ci.org/pxgamer/devrant-php.svg?branch=master)](https://travis-ci.org/pxgamer/devrant-php) 4 | 5 | A simple PHP wrapper for utilising the [devRant](https://devrant.com) api. 6 | 7 | ## Usage 8 | 9 | __Include the class:__ 10 | - Using Composer 11 | 12 | `composer require pxgamer/devrant-php` 13 | ```php 14 | getRants(); // Get rants 60 | $devRant->getRants($searchterm); // Get rants using a search query 61 | ``` 62 | Returns false on failure, or: 63 | ```php 64 | [ 65 | 0 => Rant object, 66 | 1 => Rant object, 67 | ... 68 | ] 69 | ``` 70 | 71 | ### _Getting a single rant by its id_ 72 | ```php 73 | $devRant = new \pxgamer\devRant\Connection; 74 | $devRant->getRantById(int); 75 | ``` 76 | Returns false on failure, or a `Rant` object. 77 | 78 | ### _Getting a user by their id_ 79 | ```php 80 | $devRant = new \pxgamer\devRant\Connection; 81 | $devRant->getUserById(int); 82 | ``` 83 | Returns: 84 | ```php 85 | [ 86 | "success" => true, 87 | "profile" => [ 88 | "username" => "", 89 | "score" => 0, 90 | "about" => "", 91 | "location" => "", 92 | "created_time" => 1474613872, 93 | "skills" => "", 94 | "github" => "", 95 | "website" => "", 96 | "content" => [ 97 | "content" => [ 98 | "rants" => [], 99 | "upvoted" => [], 100 | "comments" => [], 101 | "favorites" => [] 102 | [, 103 | "counts" => [] 104 | ], 105 | "avatar" => [] 106 | ] 107 | ] 108 | ``` 109 | 110 | ### _Search rants_ 111 | ```php 112 | $devRant = new \pxgamer\devRant\Connection; 113 | $devRant->getRants('string'); 114 | ``` 115 | Returns false on failure, or: 116 | ```php 117 | [ 118 | 0 => Rant object [ 119 | "id" => 0, 120 | "text" => "string", 121 | "num_upvotes" => 0, 122 | "num_downvotes" => 0, 123 | "score" => 0, 124 | "created_time" => 0, 125 | "attached_image" => [ 126 | "url" => "string", 127 | "width" => 0, 128 | "height" => 0 129 | ], 130 | "num_comments" => 0, 131 | "tags" => [ 132 | "string" 133 | ], 134 | "vote_state" => 0, 135 | "edited" => false, 136 | "user_id" => 0, 137 | "user_username" => "string", 138 | "user_score" => 0, 139 | "user_avatar" => [ 140 | "b" => "string" 141 | ] 142 | ], 143 | 1 => Rant object, 144 | ... 145 | ] 146 | ``` 147 | 148 | ### _Getting a user's id from their username_ 149 | ```php 150 | $devRant = new \pxgamer\devRant\Connection; 151 | $devRant->getUserId('username'); 152 | ``` 153 | Returns: 154 | ```php 155 | [ 156 | "success" => true, 157 | "user_id" => 0 158 | ] 159 | ``` 160 | 161 | ### _Getting signed in_ 162 | ```php 163 | $devRant = new \pxgamer\devRant\Connection; 164 | $devRant->login('username', 'password'); 165 | ``` 166 | Returns `true` if successful, `false` if not 167 | 168 | ### _Posting a rant_ 169 | ```php 170 | use \pxgamer\devRant\Rant; 171 | 172 | $devRant = new \pxgamer\devRant\Connection; 173 | if ($devRant->login('username', 'password')) { 174 | $devRant->rant(new Rant($rant_content, $tags)); 175 | } 176 | ``` 177 | Returns: 178 | ```php 179 | [ 180 | "success" => true, 181 | "rant_id" => 31131 182 | ] 183 | ``` 184 | 185 | ### _Posting a comment_ 186 | ```php 187 | $devRant = new \pxgamer\devRant\Connection; 188 | if ($devRant->login('username', 'password')) { 189 | $devRant->comment($rantId, 'Comment Content'); 190 | } 191 | ``` 192 | Returns: 193 | ```php 194 | [ 195 | "success" => true 196 | ] 197 | ``` 198 | 199 | ### _Getting Collabs_ 200 | ```php 201 | $devRant = new \pxgamer\devRant\Connection; 202 | 203 | $collabs = $devRant->collabs(); 204 | ``` 205 | Returns: 206 | ```php 207 | [ 208 | "success" => true, 209 | "rants" => [ 210 | [0] => [ 211 | ... 212 | ] 213 | ] 214 | ] 215 | ``` 216 | 217 | ### _Voting on Rants_ 218 | ```php 219 | $devRant = new \pxgamer\devRant\Connection; 220 | if ($devRant->login('username', 'password')) { 221 | $voteRant = $devRant->voteRant($rantId, $vote); 222 | } 223 | ``` 224 | Returns: 225 | ```php 226 | [ 227 | "success" => true, 228 | "rant" => [ 229 | [id] => ..., 230 | ... 231 | ] 232 | ] 233 | ``` 234 | 235 | ### _Voting on Comments_ 236 | ```php 237 | $devRant = new \pxgamer\devRant\Connection; 238 | if ($devRant->login('username', 'password')) { 239 | $voteRant = $devRant->voteComment($commentId, $vote); 240 | } 241 | ``` 242 | Returns: 243 | ```php 244 | [ 245 | "success" => true, 246 | "comment" => [ 247 | [id] => ..., 248 | ... 249 | ] 250 | ] 251 | ``` 252 | 253 | ### _Getting your notifications_ 254 | ```php 255 | $devRant = new \pxgamer\devRant\Connection; 256 | if ($devRant->login('username', 'password')) { 257 | $notifications = $devRant->notifs(); 258 | } 259 | ``` 260 | Returns: 261 | ```php 262 | [ 263 | "success" => true, 264 | "data" => { 265 | "items" => [ 266 | ... 267 | ], 268 | "check_time" => 11111, 269 | "username_map" => { 270 | ... 271 | } 272 | } 273 | ] 274 | ``` 275 | 276 | ### _Deleting a rant_ 277 | 278 | *Please note that this will __permanently__ delete the rant from devRant.* 279 | 280 | ```php 281 | $devRant = new \pxgamer\devRant\Connection; 282 | if ($devRant->login('username', 'password')) { 283 | $devRant->deleteRant($rantId); 284 | } 285 | ``` 286 | Returns: 287 | ```php 288 | [ 289 | "success" => true 290 | ] 291 | ``` 292 | 293 | ### _Deleting a comment_ 294 | 295 | *Please note that this will __permanently__ delete the comment from devRant.* 296 | 297 | ```php 298 | $devRant = new \pxgamer\devRant\Connection; 299 | if ($devRant->login('username', 'password')) { 300 | $devRant->deleteComment($commentId); 301 | } 302 | ``` 303 | Returns: 304 | ```php 305 | [ 306 | "success" => true 307 | ] 308 | ``` 309 | 310 | ### _Deleting your account_ 311 | 312 | *Please note that this will __permanently__ delete your account from devRant.* 313 | 314 | ```php 315 | $devRant = new \pxgamer\devRant\Connection; 316 | if ($devRant->login('username', 'password')) { 317 | $devRant->deleteAccount(); 318 | } 319 | ``` 320 | Returns: 321 | ```php 322 | [ 323 | "success" => true 324 | ] 325 | ``` 326 | 327 | ## License 328 | 329 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 330 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-minimal -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pxgamer/devrant-php", 3 | "type": "library", 4 | "description": "A simple PHP wrapper for utilising the devRant API.", 5 | "keywords": [ 6 | "php", 7 | "devRant", 8 | "api", 9 | "wrapper" 10 | ], 11 | "homepage": "https://github.com/PXgamer/devrant-php", 12 | "license": "MIT", 13 | "require": { 14 | "php": ">=5.6.0", 15 | "ext-curl": "*" 16 | }, 17 | "require-dev": { 18 | "phpunit/phpunit": "^5.7.23 || ^6.4.3" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "pxgamer\\devRant\\": "src/" 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | tests/ 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/Connection.php: -------------------------------------------------------------------------------- 1 | get($get); 38 | if (!isset($data['success'])) { 39 | return false; 40 | } 41 | 42 | $converter = function ($rant) { 43 | return (new Rant())->populateFrom($rant); 44 | }; 45 | 46 | return array_map($converter, 47 | (empty($term)) ? $data['rants'] : $data['results'] 48 | ); 49 | } 50 | 51 | /** 52 | * @param $id 53 | * @return bool|string 54 | */ 55 | public function getRantById($id) 56 | { 57 | if (!is_numeric($id)) { 58 | return false; 59 | } 60 | 61 | $data = $this->get('/devrant/rants/' . $id); 62 | 63 | return (isset($data['success'])) 64 | ? (new Rant())->populateFrom($data) 65 | : false; 66 | } 67 | 68 | /** 69 | * @param $id 70 | * @return bool|string 71 | */ 72 | public function getUserById($id) 73 | { 74 | return (is_numeric($id)) ? $this->get('/users/' . $id) : false; 75 | } 76 | 77 | /** 78 | * @param $username 79 | * @return bool|string 80 | */ 81 | public function getUserId($username) 82 | { 83 | return (is_string($username) && $username !== '') 84 | ? $this->get('/get-user-id?username=' . urlencode($username)) 85 | : false; 86 | } 87 | 88 | /** 89 | * @param $username 90 | * @param $password 91 | * @return bool|string 92 | */ 93 | public function login($username, $password) 94 | { 95 | if (!is_string($username) || $username === '') { 96 | return false; 97 | } 98 | 99 | $result = $this->post( 100 | '/users/auth-token', 101 | ['username' => $username, 'password' => $password] 102 | ); 103 | 104 | if ($result['success'] === true) { 105 | $this->authUserId = $result['auth_token']['user_id']; 106 | $this->tokenId = $result['auth_token']['id']; 107 | $this->tokenKey = $result['auth_token']['key']; 108 | return true; 109 | } 110 | 111 | return false; 112 | } 113 | 114 | public function logout() 115 | { 116 | $this->authUserId = 0; 117 | $this->tokenId = 0; 118 | $this->tokenKey = ''; 119 | } 120 | 121 | /** 122 | * @param Rant $rant 123 | * @return bool|string 124 | */ 125 | public function rant(Rant $rant) 126 | { 127 | if ($this->tokenId === 0 || !is_string($rant->text) 128 | || $rant->text === '' 129 | ) { 130 | return false; 131 | } 132 | 133 | return $this->post('/devrant/rants', [ 134 | 'rant' => $rant->text, 135 | 'user_id' => $this->authUserId, 136 | 'token_id' => $this->tokenId, 137 | 'token_key' => $this->tokenKey, 138 | 'tags' => $rant->tags, 139 | ]); 140 | } 141 | 142 | /** 143 | * @param $rantId 144 | * @param $comment 145 | * @return bool|string 146 | */ 147 | public function comment($rantId, $comment) 148 | { 149 | if ($this->tokenId === 0 || !is_string($comment) 150 | || $comment === '' 151 | ) { 152 | return false; 153 | } 154 | 155 | return $this->post('/devrant/rants/' . $rantId . '/comments', [ 156 | 'comment' => $comment, 157 | 'user_id' => $this->authUserId, 158 | 'token_id' => $this->tokenId, 159 | 'token_key' => $this->tokenKey, 160 | ]); 161 | } 162 | 163 | /** 164 | * @param int $rantId 165 | * @param int $vote 166 | * @return bool|string 167 | */ 168 | public function voteRant($rantId, $vote = 1) 169 | { 170 | if ($this->tokenId === 0 || !is_numeric($vote)) { 171 | return false; 172 | } 173 | 174 | return $this->post('/devrant/rants/' . $rantId . '/vote', [ 175 | 'vote' => $vote, 176 | 'user_id' => $this->authUserId, 177 | 'token_id' => $this->tokenId, 178 | 'token_key' => $this->tokenKey, 179 | ]); 180 | } 181 | 182 | /** 183 | * @param int $commentId 184 | * @param int $vote 185 | * @return bool|string 186 | */ 187 | public function voteComment($commentId, $vote = 1) 188 | { 189 | if ($this->tokenId === 0 || !is_numeric($vote)) { 190 | return false; 191 | } 192 | 193 | return $this->post('/comments/' . $commentId . '/vote', [ 194 | 'vote' => $vote, 195 | 'user_id' => $this->authUserId, 196 | 'token_id' => $this->tokenId, 197 | 'token_key' => $this->tokenKey, 198 | ]); 199 | } 200 | 201 | /** 202 | * @return bool|string 203 | */ 204 | public function notifs() 205 | { 206 | if ($this->tokenId === 0) { 207 | return false; 208 | } 209 | 210 | return $this->get('/users/me/notif-feed?user_id=' . $this->authUserId . '&token_id=' . $this->tokenId . '&token_key=' . $this->tokenKey); 211 | } 212 | 213 | /** 214 | * @return bool|string 215 | */ 216 | public function collabs() 217 | { 218 | return $this->get('/devrant/collabs?user_id=' . $this->authUserId . '&token_id=' . $this->tokenId . '&token_key=' . $this->tokenKey); 219 | } 220 | 221 | /** 222 | * @param $id 223 | * @return bool|string 224 | */ 225 | public function deleteRant($id) 226 | { 227 | if ($this->tokenId === 0 || !is_numeric($id)) { 228 | return false; 229 | } 230 | 231 | return $this->delete('/devrant/rants/' . $id . '?user_id=' . $this->authUserId . '&token_id=' . $this->tokenId . '&token_key=' . $this->tokenKey); 232 | } 233 | 234 | /** 235 | * @param $id 236 | * @return bool|string 237 | */ 238 | public function deleteComment($id) 239 | { 240 | if ($this->tokenId === 0 || !is_numeric($id)) { 241 | return false; 242 | } 243 | 244 | return $this->delete('/comments/' . $id . '?user_id=' . $this->authUserId . '&token_id=' . $this->tokenId . '&token_key=' . $this->tokenKey); 245 | } 246 | 247 | /** 248 | * @return bool|string 249 | */ 250 | public function deleteAccount() 251 | { 252 | if ($this->tokenId === 0) { 253 | return false; 254 | } 255 | 256 | return $this->delete('/users/me?user_id=' . $this->authUserId . '&token_id=' . $this->tokenId . '&token_key=' . $this->tokenKey); 257 | } 258 | 259 | /** 260 | * @param $endpoint 261 | * @return mixed 262 | */ 263 | private function get($endpoint) 264 | { 265 | $url = (strpos($endpoint, '?') == 0) 266 | ? self::API_BASE . $endpoint . '?app=' . self::APP_ID 267 | : self::API_BASE . $endpoint . '&app=' . self::APP_ID; 268 | 269 | $ch = curl_init(); 270 | curl_setopt_array( 271 | $ch, 272 | [ 273 | CURLOPT_URL => $url, 274 | CURLOPT_RETURNTRANSFER => 1, 275 | CURLOPT_FOLLOWLOCATION => 1, 276 | ] 277 | ); 278 | $result = curl_exec($ch); 279 | curl_close($ch); 280 | 281 | return json_decode($result, true); 282 | } 283 | 284 | /** 285 | * @param $endpoint 286 | * @return mixed 287 | */ 288 | private function post($endpoint, $content) 289 | { 290 | $post_array = [ 291 | 'app' => self::APP_ID, 292 | 'plat' => 3, 293 | ]; 294 | 295 | if (count($content) > 0) { 296 | $post_array = array_merge($post_array, $content); 297 | } 298 | 299 | $url = self::API_BASE . $endpoint; 300 | $ch = curl_init(); 301 | curl_setopt_array( 302 | $ch, 303 | [ 304 | CURLOPT_URL => $url, 305 | CURLOPT_RETURNTRANSFER => 1, 306 | CURLOPT_POST => 1, 307 | CURLOPT_POSTFIELDS => http_build_query($post_array), 308 | ] 309 | ); 310 | $result = curl_exec($ch); 311 | curl_close($ch); 312 | 313 | return json_decode($result, true); 314 | } 315 | 316 | /** 317 | * @param $endpoint 318 | * @return mixed 319 | */ 320 | private function delete($endpoint) 321 | { 322 | $url = (strpos($endpoint, '?') == 0) 323 | ? self::API_BASE . $endpoint . '?app=' . self::APP_ID 324 | : self::API_BASE . $endpoint . '&app=' . self::APP_ID; 325 | 326 | $ch = curl_init(); 327 | curl_setopt_array( 328 | $ch, 329 | [ 330 | CURLOPT_URL => $url, 331 | CURLOPT_RETURNTRANSFER => 1, 332 | CURLOPT_CUSTOMREQUEST => 'DELETE', 333 | ] 334 | ); 335 | $result = curl_exec($ch); 336 | curl_close($ch); 337 | 338 | return json_decode($result, true); 339 | } 340 | } 341 | -------------------------------------------------------------------------------- /src/Rant.php: -------------------------------------------------------------------------------- 1 | text = $text; 30 | $this->tags = $tags; 31 | } 32 | 33 | public function __get($var) 34 | { 35 | return $this->$var; 36 | } 37 | 38 | public function populateFrom($rant) 39 | { 40 | unset($rant['success']); 41 | array_walk($rant, function ($val, $key) { 42 | $this->$key = $val; 43 | }); 44 | return $this; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/MainTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(Connection::class, $devRant); 13 | } 14 | 15 | public function testCanGetRants() 16 | { 17 | $devRant = new Connection; 18 | $rants = $devRant->getRants(); 19 | $this->assertInternalType('array', $rants); 20 | $this->assertNotEmpty($rants); 21 | } 22 | 23 | public function testCanGetRantById() 24 | { 25 | $devRant = new Connection; 26 | $rantData = $devRant->getRantById(938509); 27 | $this->assertInstanceOf(Rant::class, $rantData); 28 | } 29 | 30 | public function testCanGetUserById() 31 | { 32 | $devRant = new Connection; 33 | $userData = $devRant->getUserById(653921); 34 | $this->assertInternalType('array', $userData); 35 | $this->assertArrayHasKey('success', $userData); 36 | } 37 | 38 | public function testCanSearchRants() 39 | { 40 | $devRant = new Connection; 41 | $searchData = $devRant->getRants('Linux'); 42 | $this->assertInternalType('array', $searchData); 43 | $this->assertNotEmpty($searchData); 44 | } 45 | 46 | public function testCanGetUserId() 47 | { 48 | $devRant = new Connection; 49 | $userIdData = $devRant->getUserId('pxgamer'); 50 | $this->assertInternalType('array', $userIdData); 51 | $this->assertArrayHasKey('success', $userIdData); 52 | } 53 | 54 | public function testCanGetCollabs() 55 | { 56 | $devRant = new Connection; 57 | $collabs = $devRant->collabs(); 58 | $this->assertInternalType('array', $collabs); 59 | $this->assertArrayHasKey('success', $collabs); 60 | } 61 | } 62 | --------------------------------------------------------------------------------