| @@ -1,0 +1,228 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * PHP Client class to interface with Owly's REST-based API | |
| 4 | + * @see http://ow.ly/api-docs | |
| 5 | + * | |
| 6 | + * Currently only supports the follow methods | |
| 7 | + * shorten - Shorten a URL | |
| 8 | + * | |
| 9 | + * Based on the Zend Ow.ly URL helper by Maxime Parmentier <maxime.parmentier@invokemedia.com> | |
| 10 | + * Support can be logged at https://github.com/invokemedia/owly-api-php | |
| 11 | + * | |
| 12 | + * @version 1.0.0 | |
| 13 | + * @author Shih Oon Liong <shihoon.liong@invokemedia.com> | |
| 14 | + * @created 24/07/2012 | |
| 15 | + * @copyright Invoke Media / Biplane | |
| 16 | + * @license http://opensource.org/licenses/mit-license.php MIT | |
| 17 | + * | |
| 18 | + * @example example.php | |
| 19 | + * | |
| 20 | + */ | |
| 21 | + | |
| 22 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 23 | + | |
| 24 | + die( 'These aren\'t the droids you\'re looking for.' ); | |
| 25 | +} | |
| 26 | + | |
| 27 | +if ( ! class_exists( 'SuextOwly' ) ) { | |
| 28 | + | |
| 29 | + class SuextOwly { | |
| 30 | + | |
| 31 | + private static $base_api_url = '//ow.ly/api/'; | |
| 32 | + | |
| 33 | + private | |
| 34 | + $apiKey = null, | |
| 35 | + $version = "1.1", | |
| 36 | + $protocol = 'http:', | |
| 37 | + $apiCalls = array ( | |
| 38 | + 'url-shorten' => 'url/shorten', | |
| 39 | + 'url-expand' => 'url/expand', | |
| 40 | + 'url-info' => 'url/info', | |
| 41 | + 'url-stats-click' => 'url/clickStats', | |
| 42 | + 'photo-upload' => 'photo/upload', | |
| 43 | + 'doc-upload' => 'doc/upload', | |
| 44 | + ); | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Constructor | |
| 48 | + * @param array $options An array of options for the class. Possible options: | |
| 49 | + * 'key' - The ow.ly API Key | |
| 50 | + * 'version' - (optional) The version number of the API to use. | |
| 51 | + * By default, it will use version 1.1. | |
| 52 | + * 'protocol' - (optional) The protocol to use when talking to the API. | |
| 53 | + * By default it will use 'http:'. | |
| 54 | + * To set it to secure, use 'https:' | |
| 55 | + * @return void | |
| 56 | + */ | |
| 57 | + public function __construct( $options ) { | |
| 58 | + | |
| 59 | + try { | |
| 60 | + | |
| 61 | + if ( ! array_key_exists( 'key', $options ) || empty( $options['key'] ) ) { | |
| 62 | + | |
| 63 | + $key_msg = __( 'API key missing.', 'wpsso' ); | |
| 64 | + | |
| 65 | + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'Ow.ly', $key_msg ); | |
| 66 | + | |
| 67 | + throw new WpssoErrorException( $except_msg ); | |
| 68 | + | |
| 69 | + } else { | |
| 70 | + | |
| 71 | + $this->apiKey = $options['key']; | |
| 72 | + | |
| 73 | + if ( array_key_exists( 'version', $options ) ) { | |
| 74 | + | |
| 75 | + $this->version = $options[ 'version' ]; | |
| 76 | + } | |
| 77 | + | |
| 78 | + if ( array_key_exists( 'protocol', $options ) ) { | |
| 79 | + | |
| 80 | + $this->version = $options['protocol']; | |
| 81 | + } | |
| 82 | + } | |
| 83 | + | |
| 84 | + } catch ( WpssoErrorException $e ) { | |
| 85 | + | |
| 86 | + $e->errorMessage(); | |
| 87 | + } | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Factory constructor | |
| 92 | + * @see SuextOwly::__constructor | |
| 93 | + * @param type $options | |
| 94 | + * @return SuextOwly | |
| 95 | + */ | |
| 96 | + public static function factory( $options ) { | |
| 97 | + | |
| 98 | + $classname = __CLASS__; | |
| 99 | + | |
| 100 | + $instance = new $classname( $options ); | |
| 101 | + | |
| 102 | + return $instance; | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Get the API Method path to use | |
| 107 | + * @param type $apiCall | |
| 108 | + * @return type | |
| 109 | + */ | |
| 110 | + private function getApiMethod( $apiCall ) { | |
| 111 | + | |
| 112 | + $url = $this->protocol . self::$base_api_url . $this->version.'/'; | |
| 113 | + | |
| 114 | + if ( ! empty( $apiCall ) ) { | |
| 115 | + | |
| 116 | + $url .= $this->apiCalls[$apiCall]; | |
| 117 | + } | |
| 118 | + | |
| 119 | + return $url; | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * Given a full URL, returns an ow.ly short URL. | |
| 124 | + * Currently the API only supports shortening a single URL per API call. | |
| 125 | + * | |
| 126 | + * @see http://ow.ly/api-docs#shorten | |
| 127 | + * @param type $longUrl The URL to shorten. Must be a valid URL | |
| 128 | + * @return mixed | |
| 129 | + * @throws WpssoErrorException | |
| 130 | + */ | |
| 131 | + public function shorten( $longUrl ) { | |
| 132 | + | |
| 133 | + try { | |
| 134 | + | |
| 135 | + $apiUrl = $this->getApiMethod( 'url-shorten' ); | |
| 136 | + $params = array( 'longUrl' => $longUrl ); | |
| 137 | + $response = $this->send( $apiUrl, $params ); | |
| 138 | + | |
| 139 | + if ( ! empty( $response->shortUrl ) ) { | |
| 140 | + | |
| 141 | + return $response->shortUrl; | |
| 142 | + } | |
| 143 | + | |
| 144 | + } catch ( WpssoErrorException $e ) { | |
| 145 | + | |
| 146 | + $e->errorMessage(); | |
| 147 | + } | |
| 148 | + | |
| 149 | + return false; | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * Do an API call on Ow.ly | |
| 154 | + * @param type $apiUrl | |
| 155 | + * @return mixed | |
| 156 | + * @throws WpssoErrorException | |
| 157 | + */ | |
| 158 | + private function send( $apiUrl, $args = array() ) { | |
| 159 | + | |
| 160 | + try { | |
| 161 | + | |
| 162 | + $args[ 'apiKey' ] = $this->apiKey; | |
| 163 | + | |
| 164 | + $apiUrl = $apiUrl . ( strpos( $apiUrl, '?' ) === false ? '?' : '&' ) . http_build_query( $args ); | |
| 165 | + | |
| 166 | + $ch = curl_init(); | |
| 167 | + | |
| 168 | + curl_setopt( $ch, CURLOPT_URL, $apiUrl ); | |
| 169 | + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
| 170 | + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 1 ); | |
| 171 | + curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 ); | |
| 172 | + | |
| 173 | + $response = curl_exec( $ch ); | |
| 174 | + $curl_errnum = curl_errno( $ch ); | |
| 175 | + $curl_errmsg = curl_error( $ch ); // See https://curl.se/libcurl/c/libcurl-errors.html. | |
| 176 | + $ssl_verify = (int) curl_getinfo( $ch, CURLINFO_SSL_VERIFYRESULT ); // See https://www.php.net/manual/en/function.curl-getinfo.php. | |
| 177 | + $http_code = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE ); | |
| 178 | + | |
| 179 | + curl_close( $ch ); | |
| 180 | + | |
| 181 | + $http_success_codes = array( 200, 201 ); | |
| 182 | + | |
| 183 | + if ( ! empty( $curl_errnum ) ) { | |
| 184 | + | |
| 185 | + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'Ow.ly', $curl_errnum . ' ' . $curl_errmsg ); | |
| 186 | + | |
| 187 | + throw new WpssoErrorException( $except_msg ); | |
| 188 | + | |
| 189 | + } elseif ( ! in_array( $http_code, $http_success_codes ) ) { | |
| 190 | + | |
| 191 | + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'Ow.ly', '' ); | |
| 192 | + | |
| 193 | + throw new WpssoErrorException( $except_msg, $http_code ); | |
| 194 | + | |
| 195 | + } else { | |
| 196 | + | |
| 197 | + $resp_data = json_decode( $response, $assoc = false ); | |
| 198 | + | |
| 199 | + if ( null === $resp_data ) { | |
| 200 | + | |
| 201 | + $json_msg = sprintf( __( 'JSON response decode error code %d.', 'wpsso' ), json_last_error() ); | |
| 202 | + | |
| 203 | + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'Ow.ly', $json_msg ); | |
| 204 | + | |
| 205 | + throw new WpssoErrorException( $except_msg ); | |
| 206 | + | |
| 207 | + } elseif ( ! empty( $resp_data->error ) ) { | |
| 208 | + | |
| 209 | + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'Ow.ly', $resp_data->error ); | |
| 210 | + | |
| 211 | + throw new WpssoErrorException( $except_msg ); | |
| 212 | + | |
| 213 | + } else { | |
| 214 | + | |
| 215 | + return $resp_data->results; | |
| 216 | + } | |
| 217 | + | |
| 218 | + } | |
| 219 | + | |
| 220 | + } catch ( WpssoErrorException $e ) { | |
| 221 | + | |
| 222 | + $e->errorMessage(); | |
| 223 | + } | |
| 224 | + | |
| 225 | + return false; | |
| 226 | + } | |
| 227 | + } | |
| 228 | +} | |