├── LICENSE.md ├── README.md ├── composer.json ├── sample.php └── src └── Pocket.php /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License # 2 | 3 | Copyright (c) 2013 [Dan Chen] (https://github.com/djchen) 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, 9 | copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the 11 | Software is furnished to do so, subject to the following 12 | conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | OTHER DEALINGS IN THE SOFTWARE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | pocket-api-php [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE) 2 | ========== 3 | 4 | A PHP wrapper for interfacing with Pocket's API (getpocket.com) 5 | 6 | 7 | ## Install 8 | 9 | composer require djchen/pocket-api-php dev-master 10 | 11 | ## Requirements 12 | 13 | - PHP >= 5.3.0 14 | - composer (getcomposer.org) 15 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "djchen/pocket-api-php", 3 | "type": "library", 4 | "description": "A PHP wrapper for interfacing with Pocket's API (getpocket.com)", 5 | "keywords": ["pocket","api"], 6 | "homepage": "https://github.com/djchen/pocket-api-php", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Dan Chen", 11 | "email": "dan@djc.me" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.3.0" 16 | }, 17 | "autoload": { 18 | "psr-0": { 19 | "Pocket": "src" 20 | } 21 | }, 22 | "minimum-stability": "stable" 23 | } 24 | -------------------------------------------------------------------------------- /sample.php: -------------------------------------------------------------------------------- 1 |
'; 16 | 17 | require('Pocket.php'); 18 | 19 | $params = array( 20 | 'consumerKey' => '' // fill in your Pocket App Consumer Key 21 | ); 22 | 23 | if (empty($params['consumerKey'])) { 24 | die('Please fill in your Pocket App Consumer Key'); 25 | } 26 | 27 | $pocket = new Pocket($params); 28 | 29 | if (isset($_GET['authorized'])) { 30 | // Convert the requestToken into an accessToken 31 | // Note that a requestToken can only be covnerted once 32 | // Thus refreshing this page will generate an auth error 33 | $user = $pocket->convertToken($_GET['authorized']); 34 | /* 35 | $user['access_token'] the user's access token for calls to Pocket 36 | $user['username'] the user's pocket username 37 | */ 38 | print_r($user); 39 | 40 | // Set the user's access token to be used for all subsequent calls to the Pocket API 41 | $pocket->setAccessToken($user['access_token']); 42 | 43 | echo NEWLINE; 44 | 45 | // Add a url to the user's pocket 46 | // http://getpocket.com/developer/docs/v3/add for a list of params 47 | $params = array( 48 | 'url' => 'https://github.com/djchen/', // required 49 | 'tags' => 'github' 50 | ); 51 | print_r($pocket->add($params, $user['access_token'])); 52 | 53 | echo NEWLINE; 54 | 55 | // Retrieve the user's list of unread items (limit 5) 56 | // http://getpocket.com/developer/docs/v3/retrieve for a list of params 57 | $params = array( 58 | 'state' => 'unread', 59 | 'sort' => 'newest', 60 | 'detailType' => 'simple', 61 | 'count' => 5 62 | ); 63 | $items = $pocket->retrieve($params, $user['access_token']); 64 | print_r($items); 65 | 66 | } else { 67 | // Attempt to detect the url of the current page to redirect back to 68 | // Normally you wouldn't do this 69 | $redirect = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?authorized='; 70 | 71 | // Request a token from Pocket 72 | $result = $pocket->requestToken($redirect); 73 | /* 74 | $result['redirect_uri'] this is the URL to send the user to getpocket.com to authorize your app 75 | $result['request_token'] this is the request_token which you will need to use to 76 | obtain the user's access token after they have authorized your app 77 | */ 78 | 79 | /* 80 | This is a hack to redirect back to us with the requestToken 81 | Normally you should save the 'request_token' in a session so it can be 82 | retrieved when the user is redirected back to you 83 | */ 84 | $result['redirect_uri'] = str_replace( 85 | urlencode('?authorized='), 86 | urlencode('?authorized=' . $result['request_token']), 87 | $result['redirect_uri'] 88 | ); 89 | // END HACK 90 | 91 | header('Location: ' . $result['redirect_uri']); 92 | } 93 | -------------------------------------------------------------------------------- /src/Pocket.php: -------------------------------------------------------------------------------- 1 | 'https://getpocket.com/v3', 37 | 'consumerKey' => null, 38 | 'accessToken' => null, 39 | 'debug' => false 40 | ); 41 | 42 | private static $_statusCodes = array( 43 | 400 => 'Invalid request, please make sure you follow the documentation for proper syntax', 44 | 401 => 'Problem authenticating the user', 45 | 403 => 'User was authenticated, but access denied due to lack of permission or rate limiting', 46 | 503 => 'Pocket\'s sync server is down for scheduled maintenance' 47 | ); 48 | 49 | /** 50 | * Constructor 51 | * 52 | * @param array $settings Array of settings with consumerKey being required 53 | * - consumerKey : required 54 | * - accessToken : optional 55 | * - apiUrl : optional 56 | * - debug : optional 57 | * 58 | * @return void 59 | */ 60 | public function __construct($settings) { 61 | foreach ($settings as $setting => $value) { 62 | if (!array_key_exists($setting, $this->_config)) { 63 | throw new PocketException('Error unknown configuration setting: ' . $setting); 64 | } 65 | $this->_config[$setting] = $value; 66 | } 67 | if ($this->_config['consumerKey'] == null) { 68 | throw new PocketException('Error: Application Consumer Key not provided'); 69 | } 70 | } 71 | 72 | public function setAccessToken($accessToken) { 73 | $this->_config['accessToken'] = $accessToken; 74 | } 75 | 76 | public function requestToken($redirectUri, $state = false) { 77 | $params = array(); 78 | $params['redirect_uri'] = $redirectUri; 79 | if ($state != false) { 80 | $params['state'] = $state; 81 | } 82 | $result = $this->_request('/oauth/request', $params); 83 | $query = array( 84 | 'request_token' => $result['code'], 85 | 'redirect_uri' => $redirectUri 86 | ); 87 | 88 | $query['redirect_uri'] = 'https://getpocket.com/auth/authorize?' . http_build_query($query); 89 | return $query; 90 | } 91 | 92 | public function convertToken($token) { 93 | $params = array(); 94 | $params['code'] = $token; 95 | $result = $this->_request('/oauth/authorize', $params); 96 | return $result; 97 | } 98 | 99 | /** 100 | * Retrieve a user’s list of items with optional filters 101 | * 102 | * @param array $params List of parameters (optional) 103 | * @param bool $accessToken The user's access token (optional) 104 | * 105 | * @return array Response from Pocket 106 | * @throws PocketException 107 | */ 108 | public function retrieve($params = array(), $accessToken = true) { 109 | return $this->_request('/get', $params, $accessToken); 110 | } 111 | 112 | /** 113 | * Sets the persistent storage handler 114 | * 115 | * @param array $params List of parameters 116 | * @param bool $accessToken The user's access token (optional) 117 | * 118 | * @return array Response from Pocket 119 | * @throws PocketException 120 | */ 121 | public function add($params = array(), $accessToken = true) { 122 | return $this->_request('/add', $params, $accessToken); 123 | } 124 | 125 | /** 126 | * Sends a single event or multiple events(e.g. add, archive, delete) and actions that will modify the user's data in one call. 127 | * 128 | * @param array $actions List of actions 129 | * @param boolean $accessToken The user's access token (optional) 130 | * 131 | * @return array Response from Pocket 132 | * @throws PocketException 133 | */ 134 | public function send($actions = array(), $accessToken = true) { 135 | $params = array(); 136 | $params['actions'] = $actions; 137 | return $this->_request('/send', $params, $accessToken); 138 | } 139 | 140 | /** 141 | * Private method that makes the HTTP call to Pocket using cURL 142 | * 143 | * @return array Response from Pocket 144 | * @throws PocketException 145 | */ 146 | private function _request($method, $params = null, $accessToken = false) { 147 | $url = $this->_config['apiUrl'] . $method; 148 | 149 | if (!$params) { 150 | $params = array(); 151 | } 152 | $params['consumer_key'] = $this->_config['consumerKey']; 153 | if ($accessToken === true) { 154 | $params['access_token'] = $this->_config['accessToken']; 155 | } else if ($accessToken !== false) { 156 | $params['access_token'] = $accessToken; 157 | } 158 | $params = json_encode($params); 159 | 160 | 161 | $c = curl_init(); 162 | curl_setopt($c, CURLOPT_URL, $url); 163 | curl_setopt($c, CURLOPT_POST, true); 164 | curl_setopt($c, CURLOPT_POSTFIELDS, $params); 165 | curl_setopt($c, CURLOPT_RETURNTRANSFER, true); 166 | curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); 167 | curl_setopt($c, CURLOPT_HEADER, $this->_config['debug']); 168 | curl_setopt($c, CURLINFO_HEADER_OUT, true); 169 | curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'X-Accept: application/json')); 170 | curl_setopt($c, CURLOPT_USERAGENT, self::CURL_USERAGENT); 171 | curl_setopt($c, CURLOPT_CONNECTTIMEOUT, self::CURL_CONNECTTIMEOUT); 172 | curl_setopt($c, CURLOPT_TIMEOUT, self::CURL_TIMEOUT); 173 | if ($this->_config['debug'] === true) { 174 | curl_setopt($c, CURLINFO_HEADER_OUT, true); 175 | } 176 | 177 | $response = curl_exec($c); 178 | 179 | if ($this->_config['debug'] === true) { 180 | $headerSize = curl_getinfo($c, CURLINFO_HEADER_SIZE); 181 | $header = substr($response, 0, $headerSize); 182 | $response = substr($response, $headerSize); 183 | 184 | echo "cURL Header:\n"; 185 | print_r(curl_getinfo($c, CURLINFO_HEADER_OUT)); 186 | echo "\n\nPOST Body:\n"; 187 | print_r($params); 188 | echo "\n\nResponse Header:\n"; 189 | print_r($header); 190 | echo "\n\nResponse Body:\n"; 191 | print_r($response); 192 | } 193 | 194 | $status = curl_getinfo($c, CURLINFO_HTTP_CODE); 195 | if ($status != 200) { 196 | if (isset(self::$_statusCodes[$status])) { 197 | throw new PocketException('Error: ' . self::$_statusCodes[$status], $status); 198 | } 199 | } 200 | 201 | curl_close($c); 202 | 203 | $result = json_decode($response, true); 204 | if (!$result) { 205 | throw new PocketException('Error could not parse response: ' . var_export($response)); 206 | } 207 | 208 | return $result; 209 | } 210 | 211 | } 212 | 213 | class PocketException extends Exception { 214 | // TODO 215 | } 216 | --------------------------------------------------------------------------------