|
3 | 3 | namespace Tinify; |
4 | 4 |
|
5 | 5 | class Client { |
6 | | - const API_ENDPOINT = 'http://webservice'; |
7 | | - |
8 | | - private $options; |
9 | | - |
10 | | - public static function userAgent() { |
11 | | - $curl = curl_version(); |
12 | | - return 'Tinify/' . VERSION . ' PHP/' . PHP_VERSION . ' curl/' . $curl['version']; |
13 | | - } |
14 | | - |
15 | | - private static function caBundle() { |
16 | | - return __DIR__ . '/../data/cacert.pem'; |
17 | | - } |
18 | | - |
19 | | - function __construct($key, $appIdentifier = null) { |
20 | | - $userAgent = join( ' ', array_filter( array( self::userAgent(), $appIdentifier) ) ); |
21 | | - $this->options = array( |
22 | | - CURLOPT_BINARYTRANSFER => true, |
23 | | - CURLOPT_RETURNTRANSFER => true, |
24 | | - CURLOPT_HEADER => true, |
25 | | - CURLOPT_USERPWD => $key ? ('api:' . $key) : null, |
26 | | - CURLOPT_USERAGENT => $userAgent, |
27 | | - ); |
28 | | - } |
29 | | - |
30 | | - function request($method, $url, $body = null, $header = array() ) { |
31 | | - if ( is_array( $body ) ) { |
32 | | - if ( ! empty( $body ) ) { |
33 | | - $body = json_encode( $body ); |
34 | | - array_push( $header, 'Content-Type: application/json' ); |
35 | | - } else { |
36 | | - $body = null; |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - $request = curl_init(); |
41 | | - curl_setopt_array( $request, $this->options ); |
42 | | - |
43 | | - $url = strtolower( substr( $url, 0, 5 ) ) == 'http:' ? $url : self::API_ENDPOINT . $url; |
44 | | - curl_setopt( $request, CURLOPT_URL, $url ); |
45 | | - curl_setopt( $request, CURLOPT_HTTPHEADER, $header ); |
46 | | - curl_setopt( $request, CURLOPT_CUSTOMREQUEST, strtoupper( $method ) ); |
47 | | - |
48 | | - if ( $body ) { |
49 | | - curl_setopt( $request, CURLOPT_POSTFIELDS, $body ); |
50 | | - } |
51 | | - |
52 | | - $response = curl_exec( $request ); |
53 | | - |
54 | | - if ( is_string( $response ) ) { |
55 | | - $status = curl_getinfo( $request, CURLINFO_HTTP_CODE ); |
56 | | - $headerSize = curl_getinfo( $request, CURLINFO_HEADER_SIZE ); |
57 | | - curl_close( $request ); |
58 | | - |
59 | | - $headers = self::parseHeaders( substr( $response, 0, $headerSize ) ); |
60 | | - $body = substr( $response, $headerSize ); |
61 | | - |
62 | | - if ( isset( $headers['compression-count'] ) ) { |
63 | | - Tinify::setCompressionCount( intval( $headers['compression-count'] ) ); |
64 | | - } |
65 | | - |
66 | | - $isJson = false; |
67 | | - if ( isset( $headers['content-type'] ) ) { |
68 | | - /* Parse JSON response bodies. */ |
69 | | - list($contentType) = explode( ';', $headers['content-type'], 2 ); |
70 | | - if ( strtolower( trim( $contentType ) ) == 'application/json' ) { |
71 | | - $isJson = true; |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - /* 1xx and 3xx are unexpected and will be treated as error. */ |
76 | | - $isError = $status <= 199 || $status >= 300; |
77 | | - |
78 | | - if ( $isJson || $isError ) { |
79 | | - /* Parse JSON bodies, always interpret errors as JSON. */ |
80 | | - $body = json_decode( $body ); |
81 | | - if ( ! $body ) { |
82 | | - $message = sprintf('Error while parsing response: %s (#%d)', |
83 | | - PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Error', |
84 | | - json_last_error()); |
85 | | - throw Exception::create( $message, 'ParseError', $status ); |
86 | | - } |
87 | | - } |
88 | | - |
89 | | - if ( $isError ) { |
90 | | - throw Exception::create( $body->message, $body->error, $status ); |
91 | | - } |
92 | | - |
93 | | - return (object) array( 'body' => $body, 'headers' => $headers ); |
94 | | - } else { |
95 | | - $message = sprintf( '%s (#%d)', curl_error( $request ), curl_errno( $request ) ); |
96 | | - curl_close( $request ); |
97 | | - throw new ConnectionException( 'Error while connecting: ' . $message ); |
98 | | - } |
99 | | - } |
100 | | - |
101 | | - protected static function parseHeaders($headers) { |
102 | | - if ( ! is_array( $headers ) ) { |
103 | | - $headers = explode( "\r\n", $headers ); |
104 | | - } |
105 | | - |
106 | | - $result = array(); |
107 | | - foreach ( $headers as $header ) { |
108 | | - if ( empty( $header ) ) { |
109 | | - continue; |
110 | | - } |
111 | | - |
112 | | - $split = explode( ':', $header, 2 ); |
113 | | - if ( count( $split ) === 2 ) { |
114 | | - $result[strtolower( $split[0] )] = trim( $split[1] ); |
115 | | - } |
116 | | - } |
117 | | - return $result; |
118 | | - } |
| 6 | + const API_ENDPOINT = "https://api.tinify.com"; |
| 7 | + |
| 8 | + protected $options; |
| 9 | + |
| 10 | + public static function userAgent() { |
| 11 | + $curl = curl_version(); |
| 12 | + return "Tinify/" . VERSION . " PHP/" . PHP_VERSION . " curl/" . $curl["version"]; |
| 13 | + } |
| 14 | + |
| 15 | + private static function caBundle() { |
| 16 | + return __DIR__ . "/../data/cacert.pem"; |
| 17 | + } |
| 18 | + |
| 19 | + function __construct($key, $appIdentifier = NULL) { |
| 20 | + $userAgent = join(" ", array_filter(array(self::userAgent(), $appIdentifier))); |
| 21 | + $this->options = array( |
| 22 | + CURLOPT_BINARYTRANSFER => true, |
| 23 | + CURLOPT_RETURNTRANSFER => true, |
| 24 | + CURLOPT_HEADER => true, |
| 25 | + CURLOPT_USERPWD => $key ? ("api:" . $key) : NULL, |
| 26 | + CURLOPT_CAINFO => self::caBundle(), |
| 27 | + CURLOPT_SSL_VERIFYPEER => true, |
| 28 | + CURLOPT_USERAGENT => $userAgent, |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + function request($method, $url, $body = NULL, $header = array()) { |
| 33 | + if (is_array($body)) { |
| 34 | + if (!empty($body)) { |
| 35 | + $body = json_encode($body); |
| 36 | + array_push($header, "Content-Type: application/json"); |
| 37 | + } else { |
| 38 | + $body = NULL; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + $request = curl_init(); |
| 43 | + curl_setopt_array($request, $this->options); |
| 44 | + |
| 45 | + $url = strtolower(substr($url, 0, 6)) == "https:" ? $url : self::API_ENDPOINT . $url; |
| 46 | + curl_setopt($request, CURLOPT_URL, $url); |
| 47 | + curl_setopt($request, CURLOPT_CUSTOMREQUEST, strtoupper($method)); |
| 48 | + |
| 49 | + if (count($header) > 0) { |
| 50 | + curl_setopt($request, CURLOPT_HTTPHEADER, $header); |
| 51 | + } |
| 52 | + |
| 53 | + if ($body) { |
| 54 | + curl_setopt($request, CURLOPT_POSTFIELDS, $body); |
| 55 | + } |
| 56 | + |
| 57 | + $response = curl_exec($request); |
| 58 | + |
| 59 | + if (is_string($response)) { |
| 60 | + $status = curl_getinfo($request, CURLINFO_HTTP_CODE); |
| 61 | + $headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE); |
| 62 | + curl_close($request); |
| 63 | + |
| 64 | + $headers = self::parseHeaders(substr($response, 0, $headerSize)); |
| 65 | + $body = substr($response, $headerSize); |
| 66 | + |
| 67 | + if (isset($headers["compression-count"])) { |
| 68 | + Tinify::setCompressionCount(intval($headers["compression-count"])); |
| 69 | + } |
| 70 | + |
| 71 | + $isJson = false; |
| 72 | + if (isset($headers["content-type"])) { |
| 73 | + /* Parse JSON response bodies. */ |
| 74 | + list($contentType) = explode(";", $headers["content-type"], 2); |
| 75 | + if (strtolower(trim($contentType)) == "application/json") { |
| 76 | + $isJson = true; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /* 1xx and 3xx are unexpected and will be treated as error. */ |
| 81 | + $isError = $status <= 199 || $status >= 300; |
| 82 | + |
| 83 | + if ($isJson || $isError) { |
| 84 | + /* Parse JSON bodies, always interpret errors as JSON. */ |
| 85 | + $body = json_decode($body); |
| 86 | + if (!$body) { |
| 87 | + $message = sprintf("Error while parsing response: %s (#%d)", |
| 88 | + PHP_VERSION_ID >= 50500 ? json_last_error_msg() : "Error", |
| 89 | + json_last_error()); |
| 90 | + throw Exception::create($message, "ParseError", $status); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if ($isError) { |
| 95 | + throw Exception::create($body->message, $body->error, $status); |
| 96 | + } |
| 97 | + |
| 98 | + return (object) array("body" => $body, "headers" => $headers); |
| 99 | + } else { |
| 100 | + $message = sprintf("%s (#%d)", curl_error($request), curl_errno($request)); |
| 101 | + curl_close($request); |
| 102 | + throw new ConnectionException("Error while connecting: " . $message); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + protected static function parseHeaders($headers) { |
| 107 | + if (!is_array($headers)) { |
| 108 | + $headers = explode("\r\n", $headers); |
| 109 | + } |
| 110 | + |
| 111 | + $result = array(); |
| 112 | + foreach ($headers as $header) { |
| 113 | + if (empty($header)) continue; |
| 114 | + $split = explode(":", $header, 2); |
| 115 | + if (count($split) === 2) { |
| 116 | + $result[strtolower($split[0])] = trim($split[1]); |
| 117 | + } |
| 118 | + } |
| 119 | + return $result; |
| 120 | + } |
119 | 121 | } |
0 commit comments