@@ -7,371 +7,371 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Route4Me |
| 9 | 9 | { |
| 10 | - public static $apiKey; |
|
| 11 | - public static $baseUrl = Endpoint::BASE_URL; |
|
| 12 | - |
|
| 13 | - public static function setApiKey($apiKey) |
|
| 14 | - { |
|
| 15 | - self::$apiKey = $apiKey; |
|
| 16 | - } |
|
| 17 | - |
|
| 18 | - public static function getApiKey() |
|
| 19 | - { |
|
| 20 | - return self::$apiKey; |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public static function setBaseUrl($baseUrl) |
|
| 24 | - { |
|
| 25 | - self::$baseUrl = $baseUrl; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - public static function getBaseUrl() |
|
| 29 | - { |
|
| 30 | - return self::$baseUrl; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Make request with CURL |
|
| 35 | - * |
|
| 36 | - * @since 1.2.3 changed error handling |
|
| 37 | - * @since 1.2.8 added $options['return_headers'] |
|
| 38 | - * |
|
| 39 | - * @param array $options |
|
| 40 | - * string url - HTTP URL. |
|
| 41 | - * string method - HTTP method. |
|
| 42 | - * string api_key - API key to access to route4me server. |
|
| 43 | - * array query - Array of query parameters. |
|
| 44 | - * array body - Array of body parameters. |
|
| 45 | - * string HTTPHEADER - Content type of body e.g. |
|
| 46 | - * 'Content-Type: application/json' |
|
| 47 | - * 'Content-Type: multipart/form-data' |
|
| 48 | - * array HTTPHEADERS - Array of headers. |
|
| 49 | - * string FILE - Path to uploading file. |
|
| 50 | - * array return_headers - Array of response headers to return as a result. |
|
| 51 | - * @throws Exception\ApiError |
|
| 52 | - */ |
|
| 53 | - public static function makeRequst($options) |
|
| 54 | - { |
|
| 55 | - $method = isset($options['method']) ? $options['method'] : 'GET'; |
|
| 56 | - $query = isset($options['query']) |
|
| 57 | - ? array_filter($options['query'], function ($x) { |
|
| 58 | - return !is_null($x); |
|
| 59 | - }) : []; |
|
| 60 | - |
|
| 61 | - $body = isset($options['body']) ? $options['body'] : null; |
|
| 62 | - $file = isset($options['FILE']) ? $options['FILE'] : null; |
|
| 63 | - $headers = [ |
|
| 64 | - 'User-Agent: Route4Me php-sdk', |
|
| 65 | - ]; |
|
| 66 | - |
|
| 67 | - $return_headers = (isset($options['return_headers']) ? $options['return_headers'] : null); |
|
| 68 | - |
|
| 69 | - if (isset($options['HTTPHEADER'])) { |
|
| 70 | - $headers[] = $options['HTTPHEADER']; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if (isset($options['HTTPHEADERS'])) { |
|
| 74 | - foreach ($options['HTTPHEADERS'] as $header) { |
|
| 75 | - $headers[] = $header; |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $ch = curl_init(); |
|
| 80 | - |
|
| 81 | - $url = isset($options['url']) |
|
| 82 | - ? $options['url'] . '?' . http_build_query(array_merge( |
|
| 83 | - $query, |
|
| 84 | - ['api_key' => self::getApiKey()] |
|
| 85 | - )) : ''; |
|
| 86 | - |
|
| 87 | - $baseUrl = self::getBaseUrl(); |
|
| 88 | - |
|
| 89 | - $curlOpts = [ |
|
| 90 | - CURLOPT_URL => $baseUrl.$url, |
|
| 91 | - CURLOPT_RETURNTRANSFER => true, |
|
| 92 | - CURLOPT_TIMEOUT => 120, |
|
| 93 | - CURLOPT_FOLLOWLOCATION => true, |
|
| 94 | - CURLOPT_SSL_VERIFYHOST => false, |
|
| 95 | - CURLOPT_SSL_VERIFYPEER => false, |
|
| 96 | - CURLOPT_HTTPHEADER => $headers, |
|
| 97 | - ]; |
|
| 98 | - |
|
| 99 | - curl_setopt_array($ch, $curlOpts); |
|
| 100 | - |
|
| 101 | - // read response headers if need |
|
| 102 | - $response_headers = []; |
|
| 103 | - if ($return_headers) { |
|
| 104 | - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$response_headers) { |
|
| 105 | - $len = strlen($header); |
|
| 106 | - $header = explode(':', $header, 2); |
|
| 107 | - if (count($header) >= 2) { |
|
| 108 | - $response_headers[strtolower(trim($header[0]))] = trim($header[1]); |
|
| 109 | - } |
|
| 110 | - return $len; |
|
| 111 | - }); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if (null != $file) { |
|
| 115 | - $cfile = new \CURLFile($file, '', ''); |
|
| 116 | - $body['strFilename']=$cfile; |
|
| 117 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
| 118 | - curl_setopt($ch, CURLOPT_POST, true); |
|
| 119 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|
| 120 | - } else { |
|
| 121 | - switch ($method) { |
|
| 122 | - case 'DELETE': |
|
| 123 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 124 | - break; |
|
| 125 | - case 'DELETEARRAY': |
|
| 126 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 127 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 128 | - break; |
|
| 129 | - case 'PUT': |
|
| 130 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
| 131 | - break; |
|
| 132 | - case 'POST': |
|
| 133 | - if (isset($body)) { |
|
| 134 | - if (isset($options['HTTPHEADER']) |
|
| 135 | - && strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 136 | - $bodyData = $body; |
|
| 137 | - } else { |
|
| 138 | - $bodyData = json_encode($body); |
|
| 139 | - } |
|
| 140 | - curl_setopt($ch, CURLOPT_POST, 1); |
|
| 141 | - curl_setopt($ch, CURLOPT_POSTREDIR, 7); |
|
| 142 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 143 | - } |
|
| 144 | - break; |
|
| 145 | - case 'ADD': |
|
| 146 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 147 | - break; |
|
| 148 | - case 'PATCH': |
|
| 149 | - if (isset($body)) { |
|
| 150 | - $bodyData = json_encode($body); |
|
| 151 | - if (isset($options['HTTPHEADER'])) { |
|
| 152 | - if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 153 | - $bodyData = $body; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); |
|
| 157 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 158 | - } |
|
| 159 | - break; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - if (is_numeric(array_search($method, ['DELETE', 'PUT']))) { |
|
| 163 | - if (isset($body)) { |
|
| 164 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - $result = curl_exec($ch); |
|
| 170 | - $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 171 | - curl_close($ch); |
|
| 172 | - |
|
| 173 | - $json = null; |
|
| 174 | - if (strpos($result, '<?xml') > -1) { |
|
| 175 | - $xml = simplexml_load_string($result); |
|
| 176 | - $json = self::object2array($xml); |
|
| 177 | - } else { |
|
| 178 | - $json = json_decode($result, true); |
|
| 179 | - } |
|
| 180 | - if (200 == $code || 201 == $code || 202 == $code) { |
|
| 181 | - if (isset($json['errors'])) { |
|
| 182 | - throw new ApiError(implode(', ', $json['errors']), $code, $result); |
|
| 183 | - } else { |
|
| 184 | - // return response headers if they were asked for |
|
| 185 | - if (count($response_headers) !== 0) { |
|
| 186 | - $res = [ |
|
| 187 | - 'code' => $code, |
|
| 188 | - 'data' => $json |
|
| 189 | - ]; |
|
| 190 | - foreach ($return_headers as $key => $value) { |
|
| 191 | - // most headers has char '-' but it is forbidden in PHP names replace it with '_' |
|
| 192 | - $res[strtolower(str_replace('-', '_', $value))] = |
|
| 193 | - (isset($response_headers[$value]) ? $response_headers[$value] : null); |
|
| 194 | - } |
|
| 195 | - return $res; |
|
| 196 | - } |
|
| 197 | - return $json; |
|
| 198 | - } |
|
| 199 | - } elseif (isset($code) && (!isset($result) || !$result)) { |
|
| 200 | - throw new ApiError('', $code, $result); |
|
| 201 | - } else { |
|
| 202 | - if (isset($json['messages'])) { |
|
| 203 | - $msg = ''; |
|
| 204 | - foreach ($json['messages'] as $key => $value) { |
|
| 205 | - if ($msg !== '') { |
|
| 206 | - $msg .= PHP_EOL; |
|
| 207 | - } |
|
| 208 | - $msg .= $key . ': ' . (is_array($value) ? implode(', ', $value) : $value); |
|
| 209 | - } |
|
| 210 | - throw new ApiError($msg, $code, $result); |
|
| 211 | - } elseif (isset($json['error']) && !empty($json['error'])) { |
|
| 212 | - throw new ApiError($json['error'], $code, $result); |
|
| 213 | - } elseif (isset($json['error'])) { |
|
| 214 | - $msg = ''; |
|
| 215 | - foreach ($json['errors'] as $key => $value) { |
|
| 216 | - if ($msg !== '') { |
|
| 217 | - $msg .= PHP_EOL; |
|
| 218 | - } |
|
| 219 | - $msg .= $value; |
|
| 220 | - } |
|
| 221 | - throw new ApiError($msg, $code, $result); |
|
| 222 | - } else { |
|
| 223 | - throw new ApiError($result, $code, $result); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * @param $object: JSON object |
|
| 230 | - */ |
|
| 231 | - public static function object2array($object) |
|
| 232 | - { |
|
| 233 | - return @json_decode(@json_encode($object), 1); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Prints on the screen main keys and values of the array. |
|
| 238 | - * |
|
| 239 | - * @param $results: object to be printed on the screen |
|
| 240 | - * @param $deepPrinting: if true, object will be printed recursively |
|
| 241 | - */ |
|
| 242 | - public static function simplePrint($results, $deepPrinting = null) |
|
| 243 | - { |
|
| 244 | - if (isset($results)) { |
|
| 245 | - if (is_array($results)) { |
|
| 246 | - foreach ($results as $key => $result) { |
|
| 247 | - if (is_array($result)) { |
|
| 248 | - foreach ($result as $key1 => $result1) { |
|
| 249 | - if (is_array($result1)) { |
|
| 250 | - if ($deepPrinting) { |
|
| 251 | - echo "<br>$key1 ------><br>"; |
|
| 252 | - self::simplePrint($result1, true); |
|
| 253 | - echo '------<br>'; |
|
| 254 | - } else { |
|
| 255 | - echo $key1.' --> '.'Array() <br>'; |
|
| 256 | - } |
|
| 257 | - } else { |
|
| 258 | - if (is_object($result1)) { |
|
| 259 | - if ($deepPrinting) { |
|
| 260 | - echo "<br>$key1 ------><br>"; |
|
| 261 | - $oarray = (array) $result1; |
|
| 262 | - self::simplePrint($oarray, true); |
|
| 263 | - echo '------<br>'; |
|
| 264 | - } else { |
|
| 265 | - echo $key1.' --> '.'Object <br>'; |
|
| 266 | - } |
|
| 267 | - } else { |
|
| 268 | - if (!is_null($result1)) { |
|
| 269 | - echo $key1.' --> '.$result1.'<br>'; |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - } else { |
|
| 275 | - if (is_object($result)) { |
|
| 276 | - if ($deepPrinting) { |
|
| 277 | - echo "<br>$key ------><br>"; |
|
| 278 | - $oarray = (array) $result; |
|
| 279 | - self::simplePrint($oarray, true); |
|
| 280 | - echo '------<br>'; |
|
| 281 | - } else { |
|
| 282 | - echo $key.' --> '.'Object <br>'; |
|
| 283 | - } |
|
| 284 | - } else { |
|
| 285 | - if (!is_null($result)) { |
|
| 286 | - echo $key.' --> '.$result.'<br>'; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - //echo "<br>"; |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - } |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Generates query or body parameters. |
|
| 298 | - * |
|
| 299 | - * @param $allFields: all known fields could be used for parameters generation |
|
| 300 | - * @param $params: input parameters (array or object) |
|
| 301 | - */ |
|
| 302 | - public static function generateRequestParameters($allFields, $params) |
|
| 303 | - { |
|
| 304 | - $generatedParams = []; |
|
| 305 | - |
|
| 306 | - if (is_array($params)) { |
|
| 307 | - foreach ($allFields as $field) { |
|
| 308 | - if (isset($params[$field])) { |
|
| 309 | - $generatedParams[$field] = $params[$field]; |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - } elseif (is_object($params)) { |
|
| 313 | - foreach ($allFields as $field) { |
|
| 314 | - if (isset($params->{$field})) { |
|
| 315 | - $generatedParams[$field] = $params->{$field}; |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - return $generatedParams; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Returns an array of the object properties. |
|
| 325 | - * |
|
| 326 | - * @param $object: An object |
|
| 327 | - * @param $exclude: array of the object parameters to be excluded from the returned array |
|
| 328 | - */ |
|
| 329 | - public static function getObjectProperties($object, $exclude) |
|
| 330 | - { |
|
| 331 | - $objectParameters = []; |
|
| 332 | - |
|
| 333 | - foreach (get_object_vars($object) as $key => $value) { |
|
| 334 | - if (property_exists($object, $key)) { |
|
| 335 | - if (!is_numeric(array_search($key, $exclude))) { |
|
| 336 | - array_push($objectParameters, $key); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - return $objectParameters; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Returns url path generated from the array of the fields and parameters. |
|
| 346 | - * |
|
| 347 | - * @param $allFields; array of the paossible fields (parameter names) |
|
| 348 | - * @param $params: input parameters (array or object) |
|
| 349 | - */ |
|
| 350 | - public static function generateUrlPath($allFields, $params) |
|
| 351 | - { |
|
| 352 | - $generatedPath = ''; |
|
| 353 | - |
|
| 354 | - if (is_array($params)) { |
|
| 355 | - foreach ($allFields as $field) { |
|
| 356 | - if (isset($params[$field])) { |
|
| 357 | - $generatedPath .= $params[$field].'/'; |
|
| 358 | - } |
|
| 359 | - } |
|
| 360 | - } elseif (is_object($params)) { |
|
| 361 | - foreach ($allFields as $field) { |
|
| 362 | - if (isset($params->{$field})) { |
|
| 363 | - $generatedPath .= $params->{$field}.'/'; |
|
| 364 | - } |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - return $generatedPath; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - public static function getFileRealPath($fileName) |
|
| 372 | - { |
|
| 373 | - $rpath = function_exists('curl_file_create') ? curl_file_create(realpath($fileName)) : '@'.realpath($fileName); |
|
| 374 | - |
|
| 375 | - return $rpath; |
|
| 376 | - } |
|
| 10 | + public static $apiKey; |
|
| 11 | + public static $baseUrl = Endpoint::BASE_URL; |
|
| 12 | + |
|
| 13 | + public static function setApiKey($apiKey) |
|
| 14 | + { |
|
| 15 | + self::$apiKey = $apiKey; |
|
| 16 | + } |
|
| 17 | + |
|
| 18 | + public static function getApiKey() |
|
| 19 | + { |
|
| 20 | + return self::$apiKey; |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + public static function setBaseUrl($baseUrl) |
|
| 24 | + { |
|
| 25 | + self::$baseUrl = $baseUrl; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public static function getBaseUrl() |
|
| 29 | + { |
|
| 30 | + return self::$baseUrl; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Make request with CURL |
|
| 35 | + * |
|
| 36 | + * @since 1.2.3 changed error handling |
|
| 37 | + * @since 1.2.8 added $options['return_headers'] |
|
| 38 | + * |
|
| 39 | + * @param array $options |
|
| 40 | + * string url - HTTP URL. |
|
| 41 | + * string method - HTTP method. |
|
| 42 | + * string api_key - API key to access to route4me server. |
|
| 43 | + * array query - Array of query parameters. |
|
| 44 | + * array body - Array of body parameters. |
|
| 45 | + * string HTTPHEADER - Content type of body e.g. |
|
| 46 | + * 'Content-Type: application/json' |
|
| 47 | + * 'Content-Type: multipart/form-data' |
|
| 48 | + * array HTTPHEADERS - Array of headers. |
|
| 49 | + * string FILE - Path to uploading file. |
|
| 50 | + * array return_headers - Array of response headers to return as a result. |
|
| 51 | + * @throws Exception\ApiError |
|
| 52 | + */ |
|
| 53 | + public static function makeRequst($options) |
|
| 54 | + { |
|
| 55 | + $method = isset($options['method']) ? $options['method'] : 'GET'; |
|
| 56 | + $query = isset($options['query']) |
|
| 57 | + ? array_filter($options['query'], function ($x) { |
|
| 58 | + return !is_null($x); |
|
| 59 | + }) : []; |
|
| 60 | + |
|
| 61 | + $body = isset($options['body']) ? $options['body'] : null; |
|
| 62 | + $file = isset($options['FILE']) ? $options['FILE'] : null; |
|
| 63 | + $headers = [ |
|
| 64 | + 'User-Agent: Route4Me php-sdk', |
|
| 65 | + ]; |
|
| 66 | + |
|
| 67 | + $return_headers = (isset($options['return_headers']) ? $options['return_headers'] : null); |
|
| 68 | + |
|
| 69 | + if (isset($options['HTTPHEADER'])) { |
|
| 70 | + $headers[] = $options['HTTPHEADER']; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if (isset($options['HTTPHEADERS'])) { |
|
| 74 | + foreach ($options['HTTPHEADERS'] as $header) { |
|
| 75 | + $headers[] = $header; |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $ch = curl_init(); |
|
| 80 | + |
|
| 81 | + $url = isset($options['url']) |
|
| 82 | + ? $options['url'] . '?' . http_build_query(array_merge( |
|
| 83 | + $query, |
|
| 84 | + ['api_key' => self::getApiKey()] |
|
| 85 | + )) : ''; |
|
| 86 | + |
|
| 87 | + $baseUrl = self::getBaseUrl(); |
|
| 88 | + |
|
| 89 | + $curlOpts = [ |
|
| 90 | + CURLOPT_URL => $baseUrl.$url, |
|
| 91 | + CURLOPT_RETURNTRANSFER => true, |
|
| 92 | + CURLOPT_TIMEOUT => 120, |
|
| 93 | + CURLOPT_FOLLOWLOCATION => true, |
|
| 94 | + CURLOPT_SSL_VERIFYHOST => false, |
|
| 95 | + CURLOPT_SSL_VERIFYPEER => false, |
|
| 96 | + CURLOPT_HTTPHEADER => $headers, |
|
| 97 | + ]; |
|
| 98 | + |
|
| 99 | + curl_setopt_array($ch, $curlOpts); |
|
| 100 | + |
|
| 101 | + // read response headers if need |
|
| 102 | + $response_headers = []; |
|
| 103 | + if ($return_headers) { |
|
| 104 | + curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$response_headers) { |
|
| 105 | + $len = strlen($header); |
|
| 106 | + $header = explode(':', $header, 2); |
|
| 107 | + if (count($header) >= 2) { |
|
| 108 | + $response_headers[strtolower(trim($header[0]))] = trim($header[1]); |
|
| 109 | + } |
|
| 110 | + return $len; |
|
| 111 | + }); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if (null != $file) { |
|
| 115 | + $cfile = new \CURLFile($file, '', ''); |
|
| 116 | + $body['strFilename']=$cfile; |
|
| 117 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
| 118 | + curl_setopt($ch, CURLOPT_POST, true); |
|
| 119 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|
| 120 | + } else { |
|
| 121 | + switch ($method) { |
|
| 122 | + case 'DELETE': |
|
| 123 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 124 | + break; |
|
| 125 | + case 'DELETEARRAY': |
|
| 126 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 127 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 128 | + break; |
|
| 129 | + case 'PUT': |
|
| 130 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
| 131 | + break; |
|
| 132 | + case 'POST': |
|
| 133 | + if (isset($body)) { |
|
| 134 | + if (isset($options['HTTPHEADER']) |
|
| 135 | + && strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 136 | + $bodyData = $body; |
|
| 137 | + } else { |
|
| 138 | + $bodyData = json_encode($body); |
|
| 139 | + } |
|
| 140 | + curl_setopt($ch, CURLOPT_POST, 1); |
|
| 141 | + curl_setopt($ch, CURLOPT_POSTREDIR, 7); |
|
| 142 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 143 | + } |
|
| 144 | + break; |
|
| 145 | + case 'ADD': |
|
| 146 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 147 | + break; |
|
| 148 | + case 'PATCH': |
|
| 149 | + if (isset($body)) { |
|
| 150 | + $bodyData = json_encode($body); |
|
| 151 | + if (isset($options['HTTPHEADER'])) { |
|
| 152 | + if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 153 | + $bodyData = $body; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); |
|
| 157 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 158 | + } |
|
| 159 | + break; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + if (is_numeric(array_search($method, ['DELETE', 'PUT']))) { |
|
| 163 | + if (isset($body)) { |
|
| 164 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + $result = curl_exec($ch); |
|
| 170 | + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 171 | + curl_close($ch); |
|
| 172 | + |
|
| 173 | + $json = null; |
|
| 174 | + if (strpos($result, '<?xml') > -1) { |
|
| 175 | + $xml = simplexml_load_string($result); |
|
| 176 | + $json = self::object2array($xml); |
|
| 177 | + } else { |
|
| 178 | + $json = json_decode($result, true); |
|
| 179 | + } |
|
| 180 | + if (200 == $code || 201 == $code || 202 == $code) { |
|
| 181 | + if (isset($json['errors'])) { |
|
| 182 | + throw new ApiError(implode(', ', $json['errors']), $code, $result); |
|
| 183 | + } else { |
|
| 184 | + // return response headers if they were asked for |
|
| 185 | + if (count($response_headers) !== 0) { |
|
| 186 | + $res = [ |
|
| 187 | + 'code' => $code, |
|
| 188 | + 'data' => $json |
|
| 189 | + ]; |
|
| 190 | + foreach ($return_headers as $key => $value) { |
|
| 191 | + // most headers has char '-' but it is forbidden in PHP names replace it with '_' |
|
| 192 | + $res[strtolower(str_replace('-', '_', $value))] = |
|
| 193 | + (isset($response_headers[$value]) ? $response_headers[$value] : null); |
|
| 194 | + } |
|
| 195 | + return $res; |
|
| 196 | + } |
|
| 197 | + return $json; |
|
| 198 | + } |
|
| 199 | + } elseif (isset($code) && (!isset($result) || !$result)) { |
|
| 200 | + throw new ApiError('', $code, $result); |
|
| 201 | + } else { |
|
| 202 | + if (isset($json['messages'])) { |
|
| 203 | + $msg = ''; |
|
| 204 | + foreach ($json['messages'] as $key => $value) { |
|
| 205 | + if ($msg !== '') { |
|
| 206 | + $msg .= PHP_EOL; |
|
| 207 | + } |
|
| 208 | + $msg .= $key . ': ' . (is_array($value) ? implode(', ', $value) : $value); |
|
| 209 | + } |
|
| 210 | + throw new ApiError($msg, $code, $result); |
|
| 211 | + } elseif (isset($json['error']) && !empty($json['error'])) { |
|
| 212 | + throw new ApiError($json['error'], $code, $result); |
|
| 213 | + } elseif (isset($json['error'])) { |
|
| 214 | + $msg = ''; |
|
| 215 | + foreach ($json['errors'] as $key => $value) { |
|
| 216 | + if ($msg !== '') { |
|
| 217 | + $msg .= PHP_EOL; |
|
| 218 | + } |
|
| 219 | + $msg .= $value; |
|
| 220 | + } |
|
| 221 | + throw new ApiError($msg, $code, $result); |
|
| 222 | + } else { |
|
| 223 | + throw new ApiError($result, $code, $result); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * @param $object: JSON object |
|
| 230 | + */ |
|
| 231 | + public static function object2array($object) |
|
| 232 | + { |
|
| 233 | + return @json_decode(@json_encode($object), 1); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Prints on the screen main keys and values of the array. |
|
| 238 | + * |
|
| 239 | + * @param $results: object to be printed on the screen |
|
| 240 | + * @param $deepPrinting: if true, object will be printed recursively |
|
| 241 | + */ |
|
| 242 | + public static function simplePrint($results, $deepPrinting = null) |
|
| 243 | + { |
|
| 244 | + if (isset($results)) { |
|
| 245 | + if (is_array($results)) { |
|
| 246 | + foreach ($results as $key => $result) { |
|
| 247 | + if (is_array($result)) { |
|
| 248 | + foreach ($result as $key1 => $result1) { |
|
| 249 | + if (is_array($result1)) { |
|
| 250 | + if ($deepPrinting) { |
|
| 251 | + echo "<br>$key1 ------><br>"; |
|
| 252 | + self::simplePrint($result1, true); |
|
| 253 | + echo '------<br>'; |
|
| 254 | + } else { |
|
| 255 | + echo $key1.' --> '.'Array() <br>'; |
|
| 256 | + } |
|
| 257 | + } else { |
|
| 258 | + if (is_object($result1)) { |
|
| 259 | + if ($deepPrinting) { |
|
| 260 | + echo "<br>$key1 ------><br>"; |
|
| 261 | + $oarray = (array) $result1; |
|
| 262 | + self::simplePrint($oarray, true); |
|
| 263 | + echo '------<br>'; |
|
| 264 | + } else { |
|
| 265 | + echo $key1.' --> '.'Object <br>'; |
|
| 266 | + } |
|
| 267 | + } else { |
|
| 268 | + if (!is_null($result1)) { |
|
| 269 | + echo $key1.' --> '.$result1.'<br>'; |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + } else { |
|
| 275 | + if (is_object($result)) { |
|
| 276 | + if ($deepPrinting) { |
|
| 277 | + echo "<br>$key ------><br>"; |
|
| 278 | + $oarray = (array) $result; |
|
| 279 | + self::simplePrint($oarray, true); |
|
| 280 | + echo '------<br>'; |
|
| 281 | + } else { |
|
| 282 | + echo $key.' --> '.'Object <br>'; |
|
| 283 | + } |
|
| 284 | + } else { |
|
| 285 | + if (!is_null($result)) { |
|
| 286 | + echo $key.' --> '.$result.'<br>'; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + //echo "<br>"; |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Generates query or body parameters. |
|
| 298 | + * |
|
| 299 | + * @param $allFields: all known fields could be used for parameters generation |
|
| 300 | + * @param $params: input parameters (array or object) |
|
| 301 | + */ |
|
| 302 | + public static function generateRequestParameters($allFields, $params) |
|
| 303 | + { |
|
| 304 | + $generatedParams = []; |
|
| 305 | + |
|
| 306 | + if (is_array($params)) { |
|
| 307 | + foreach ($allFields as $field) { |
|
| 308 | + if (isset($params[$field])) { |
|
| 309 | + $generatedParams[$field] = $params[$field]; |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + } elseif (is_object($params)) { |
|
| 313 | + foreach ($allFields as $field) { |
|
| 314 | + if (isset($params->{$field})) { |
|
| 315 | + $generatedParams[$field] = $params->{$field}; |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + return $generatedParams; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Returns an array of the object properties. |
|
| 325 | + * |
|
| 326 | + * @param $object: An object |
|
| 327 | + * @param $exclude: array of the object parameters to be excluded from the returned array |
|
| 328 | + */ |
|
| 329 | + public static function getObjectProperties($object, $exclude) |
|
| 330 | + { |
|
| 331 | + $objectParameters = []; |
|
| 332 | + |
|
| 333 | + foreach (get_object_vars($object) as $key => $value) { |
|
| 334 | + if (property_exists($object, $key)) { |
|
| 335 | + if (!is_numeric(array_search($key, $exclude))) { |
|
| 336 | + array_push($objectParameters, $key); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + return $objectParameters; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Returns url path generated from the array of the fields and parameters. |
|
| 346 | + * |
|
| 347 | + * @param $allFields; array of the paossible fields (parameter names) |
|
| 348 | + * @param $params: input parameters (array or object) |
|
| 349 | + */ |
|
| 350 | + public static function generateUrlPath($allFields, $params) |
|
| 351 | + { |
|
| 352 | + $generatedPath = ''; |
|
| 353 | + |
|
| 354 | + if (is_array($params)) { |
|
| 355 | + foreach ($allFields as $field) { |
|
| 356 | + if (isset($params[$field])) { |
|
| 357 | + $generatedPath .= $params[$field].'/'; |
|
| 358 | + } |
|
| 359 | + } |
|
| 360 | + } elseif (is_object($params)) { |
|
| 361 | + foreach ($allFields as $field) { |
|
| 362 | + if (isset($params->{$field})) { |
|
| 363 | + $generatedPath .= $params->{$field}.'/'; |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + return $generatedPath; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + public static function getFileRealPath($fileName) |
|
| 372 | + { |
|
| 373 | + $rpath = function_exists('curl_file_create') ? curl_file_create(realpath($fileName)) : '@'.realpath($fileName); |
|
| 374 | + |
|
| 375 | + return $rpath; |
|
| 376 | + } |
|
| 377 | 377 | } |