@@ -4,16 +4,16 @@ |
||
| 4 | 4 | |
| 5 | 5 | class ApiError extends \Exception |
| 6 | 6 | { |
| 7 | - protected string $source = ''; |
|
| 7 | + protected string $source = ''; |
|
| 8 | 8 | |
| 9 | - public function __construct(string $message = '', int $code = 0, string $source = '', \Throwable $previous = null) |
|
| 10 | - { |
|
| 11 | - parent::__construct($message, $code, $previous); |
|
| 12 | - $this->source = $source; |
|
| 13 | - } |
|
| 9 | + public function __construct(string $message = '', int $code = 0, string $source = '', \Throwable $previous = null) |
|
| 10 | + { |
|
| 11 | + parent::__construct($message, $code, $previous); |
|
| 12 | + $this->source = $source; |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - final public function getSource() : string |
|
| 16 | - { |
|
| 17 | - return $this->source; |
|
| 18 | - } |
|
| 15 | + final public function getSource() : string |
|
| 16 | + { |
|
| 17 | + return $this->source; |
|
| 18 | + } |
|
| 19 | 19 | } |
@@ -7,309 +7,309 @@ |
||
| 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 | - public static function makeRequst($options) |
|
| 34 | - { |
|
| 35 | - $method = isset($options['method']) ? $options['method'] : 'GET'; |
|
| 36 | - $query = isset($options['query']) |
|
| 37 | - ? array_filter($options['query'], function ($x) { |
|
| 38 | - return !is_null($x); |
|
| 39 | - }) : []; |
|
| 40 | - |
|
| 41 | - $body = isset($options['body']) ? $options['body'] : null; |
|
| 42 | - $file = isset($options['FILE']) ? $options['FILE'] : null; |
|
| 43 | - $headers = [ |
|
| 44 | - 'User-Agent: Route4Me php-sdk', |
|
| 45 | - ]; |
|
| 46 | - |
|
| 47 | - if (isset($options['HTTPHEADER'])) { |
|
| 48 | - $headers[] = $options['HTTPHEADER']; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - if (isset($options['HTTPHEADERS'])) { |
|
| 52 | - foreach ($options['HTTPHEADERS'] as $header) { |
|
| 53 | - $headers[] = $header; |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $ch = curl_init(); |
|
| 58 | - |
|
| 59 | - $url = isset($options['url']) |
|
| 60 | - ? $options['url'] . '?' . http_build_query(array_merge( |
|
| 61 | - $query, |
|
| 62 | - ['api_key' => self::getApiKey()] |
|
| 63 | - )) : ''; |
|
| 64 | - |
|
| 65 | - $baseUrl = self::getBaseUrl(); |
|
| 66 | - |
|
| 67 | - $curlOpts = [ |
|
| 68 | - CURLOPT_URL => $baseUrl.$url, |
|
| 69 | - CURLOPT_RETURNTRANSFER => true, |
|
| 70 | - CURLOPT_TIMEOUT => 120, |
|
| 71 | - CURLOPT_FOLLOWLOCATION => true, |
|
| 72 | - CURLOPT_SSL_VERIFYHOST => false, |
|
| 73 | - CURLOPT_SSL_VERIFYPEER => false, |
|
| 74 | - CURLOPT_HTTPHEADER => $headers, |
|
| 75 | - ]; |
|
| 76 | - |
|
| 77 | - curl_setopt_array($ch, $curlOpts); |
|
| 78 | - |
|
| 79 | - if (null != $file) { |
|
| 80 | - $cfile = new \CURLFile($file, '', ''); |
|
| 81 | - $body['strFilename']=$cfile; |
|
| 82 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
| 83 | - curl_setopt($ch, CURLOPT_POST, true); |
|
| 84 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|
| 85 | - } else { |
|
| 86 | - switch ($method) { |
|
| 87 | - case 'DELETE': |
|
| 88 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 89 | - break; |
|
| 90 | - case 'DELETEARRAY': |
|
| 91 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 92 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 93 | - break; |
|
| 94 | - case 'PUT': |
|
| 95 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
| 96 | - break; |
|
| 97 | - case 'POST': |
|
| 98 | - if (isset($body)) { |
|
| 99 | - $bodyData = json_encode($body); |
|
| 100 | - if (isset($options['HTTPHEADER'])) { |
|
| 101 | - if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 102 | - $bodyData = $body; |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 107 | - } |
|
| 108 | - break; |
|
| 109 | - case 'ADD': |
|
| 110 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 111 | - break; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if (is_numeric(array_search($method, ['DELETE', 'PUT']))) { |
|
| 115 | - if (isset($body)) { |
|
| 116 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $result = curl_exec($ch); |
|
| 122 | - $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 123 | - curl_close($ch); |
|
| 124 | - |
|
| 125 | - $json = null; |
|
| 126 | - if (strpos($result, '<?xml') > -1) { |
|
| 127 | - $xml = simplexml_load_string($result); |
|
| 128 | - $json = self::object2array($xml); |
|
| 129 | - } else { |
|
| 130 | - $json = json_decode($result, true); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if (200 == $code) { |
|
| 134 | - if (isset($json['errors'])) { |
|
| 135 | - throw new ApiError(implode(', ', $json['errors']), $code, $result); |
|
| 136 | - } else { |
|
| 137 | - return $json; |
|
| 138 | - } |
|
| 139 | - } elseif (isset($code) && (!isset($result) || !$result)) { |
|
| 140 | - throw new ApiError('', $code, $result); |
|
| 141 | - } else { |
|
| 142 | - if (isset($json['messages'])) { |
|
| 143 | - $msg = ''; |
|
| 144 | - foreach ($json['messages'] as $key => $value) { |
|
| 145 | - if ($msg !== '') { |
|
| 146 | - $msg .= PHP_EOL; |
|
| 147 | - } |
|
| 148 | - $msg .= $key . ': ' . implode(', ', $value); |
|
| 149 | - } |
|
| 150 | - throw new ApiError($msg, $code, $result); |
|
| 151 | - } elseif (isset($json['errors'])) { |
|
| 152 | - $msg = ''; |
|
| 153 | - foreach ($json['errors'] as $key => $value) { |
|
| 154 | - if ($msg !== '') { |
|
| 155 | - $msg .= PHP_EOL; |
|
| 156 | - } |
|
| 157 | - $msg .= $value; |
|
| 158 | - } |
|
| 159 | - throw new ApiError($msg, $code, $result); |
|
| 160 | - } else { |
|
| 161 | - throw new ApiError($result, $code, $result); |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param $object: JSON object |
|
| 168 | - */ |
|
| 169 | - public static function object2array($object) |
|
| 170 | - { |
|
| 171 | - return @json_decode(@json_encode($object), 1); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Prints on the screen main keys and values of the array. |
|
| 176 | - * |
|
| 177 | - * @param $results: object to be printed on the screen |
|
| 178 | - * @param $deepPrinting: if true, object will be printed recursively |
|
| 179 | - */ |
|
| 180 | - public static function simplePrint($results, $deepPrinting = null) |
|
| 181 | - { |
|
| 182 | - if (isset($results)) { |
|
| 183 | - if (is_array($results)) { |
|
| 184 | - foreach ($results as $key => $result) { |
|
| 185 | - if (is_array($result)) { |
|
| 186 | - foreach ($result as $key1 => $result1) { |
|
| 187 | - if (is_array($result1)) { |
|
| 188 | - if ($deepPrinting) { |
|
| 189 | - echo "<br>$key1 ------><br>"; |
|
| 190 | - self::simplePrint($result1, true); |
|
| 191 | - echo '------<br>'; |
|
| 192 | - } else { |
|
| 193 | - echo $key1.' --> '.'Array() <br>'; |
|
| 194 | - } |
|
| 195 | - } else { |
|
| 196 | - if (is_object($result1)) { |
|
| 197 | - if ($deepPrinting) { |
|
| 198 | - echo "<br>$key1 ------><br>"; |
|
| 199 | - $oarray = (array) $result1; |
|
| 200 | - self::simplePrint($oarray, true); |
|
| 201 | - echo '------<br>'; |
|
| 202 | - } else { |
|
| 203 | - echo $key1.' --> '.'Object <br>'; |
|
| 204 | - } |
|
| 205 | - } else { |
|
| 206 | - if (!is_null($result1)) { |
|
| 207 | - echo $key1.' --> '.$result1.'<br>'; |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - } else { |
|
| 213 | - if (is_object($result)) { |
|
| 214 | - if ($deepPrinting) { |
|
| 215 | - echo "<br>$key ------><br>"; |
|
| 216 | - $oarray = (array) $result; |
|
| 217 | - self::simplePrint($oarray, true); |
|
| 218 | - echo '------<br>'; |
|
| 219 | - } else { |
|
| 220 | - echo $key.' --> '.'Object <br>'; |
|
| 221 | - } |
|
| 222 | - } else { |
|
| 223 | - if (!is_null($result)) { |
|
| 224 | - echo $key.' --> '.$result.'<br>'; |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - //echo "<br>"; |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Generates query or body parameters. |
|
| 236 | - * |
|
| 237 | - * @param $allFields: all known fields could be used for parameters generation |
|
| 238 | - * @param $params: input parameters (array or object) |
|
| 239 | - */ |
|
| 240 | - public static function generateRequestParameters($allFields, $params) |
|
| 241 | - { |
|
| 242 | - $generatedParams = []; |
|
| 243 | - |
|
| 244 | - if (is_array($params)) { |
|
| 245 | - foreach ($allFields as $field) { |
|
| 246 | - if (isset($params[$field])) { |
|
| 247 | - $generatedParams[$field] = $params[$field]; |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - } elseif (is_object($params)) { |
|
| 251 | - foreach ($allFields as $field) { |
|
| 252 | - if (isset($params->{$field})) { |
|
| 253 | - $generatedParams[$field] = $params->{$field}; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - return $generatedParams; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Returns an array of the object properties. |
|
| 263 | - * |
|
| 264 | - * @param $object: An object |
|
| 265 | - * @param $exclude: array of the object parameters to be excluded from the returned array |
|
| 266 | - */ |
|
| 267 | - public static function getObjectProperties($object, $exclude) |
|
| 268 | - { |
|
| 269 | - $objectParameters = []; |
|
| 270 | - |
|
| 271 | - foreach (get_object_vars($object) as $key => $value) { |
|
| 272 | - if (property_exists($object, $key)) { |
|
| 273 | - if (!is_numeric(array_search($key, $exclude))) { |
|
| 274 | - array_push($objectParameters, $key); |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - return $objectParameters; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Returns url path generated from the array of the fields and parameters. |
|
| 284 | - * |
|
| 285 | - * @param $allFields; array of the paossible fields (parameter names) |
|
| 286 | - * @param $params: input parameters (array or object) |
|
| 287 | - */ |
|
| 288 | - public static function generateUrlPath($allFields, $params) |
|
| 289 | - { |
|
| 290 | - $generatedPath = ''; |
|
| 291 | - |
|
| 292 | - if (is_array($params)) { |
|
| 293 | - foreach ($allFields as $field) { |
|
| 294 | - if (isset($params[$field])) { |
|
| 295 | - $generatedPath .= $params[$field].'/'; |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - } elseif (is_object($params)) { |
|
| 299 | - foreach ($allFields as $field) { |
|
| 300 | - if (isset($params->{$field})) { |
|
| 301 | - $generatedPath .= $params->{$field}.'/'; |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - return $generatedPath; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - public static function getFileRealPath($fileName) |
|
| 310 | - { |
|
| 311 | - $rpath = function_exists('curl_file_create') ? curl_file_create(realpath($fileName)) : '@'.realpath($fileName); |
|
| 312 | - |
|
| 313 | - return $rpath; |
|
| 314 | - } |
|
| 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 | + public static function makeRequst($options) |
|
| 34 | + { |
|
| 35 | + $method = isset($options['method']) ? $options['method'] : 'GET'; |
|
| 36 | + $query = isset($options['query']) |
|
| 37 | + ? array_filter($options['query'], function ($x) { |
|
| 38 | + return !is_null($x); |
|
| 39 | + }) : []; |
|
| 40 | + |
|
| 41 | + $body = isset($options['body']) ? $options['body'] : null; |
|
| 42 | + $file = isset($options['FILE']) ? $options['FILE'] : null; |
|
| 43 | + $headers = [ |
|
| 44 | + 'User-Agent: Route4Me php-sdk', |
|
| 45 | + ]; |
|
| 46 | + |
|
| 47 | + if (isset($options['HTTPHEADER'])) { |
|
| 48 | + $headers[] = $options['HTTPHEADER']; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + if (isset($options['HTTPHEADERS'])) { |
|
| 52 | + foreach ($options['HTTPHEADERS'] as $header) { |
|
| 53 | + $headers[] = $header; |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $ch = curl_init(); |
|
| 58 | + |
|
| 59 | + $url = isset($options['url']) |
|
| 60 | + ? $options['url'] . '?' . http_build_query(array_merge( |
|
| 61 | + $query, |
|
| 62 | + ['api_key' => self::getApiKey()] |
|
| 63 | + )) : ''; |
|
| 64 | + |
|
| 65 | + $baseUrl = self::getBaseUrl(); |
|
| 66 | + |
|
| 67 | + $curlOpts = [ |
|
| 68 | + CURLOPT_URL => $baseUrl.$url, |
|
| 69 | + CURLOPT_RETURNTRANSFER => true, |
|
| 70 | + CURLOPT_TIMEOUT => 120, |
|
| 71 | + CURLOPT_FOLLOWLOCATION => true, |
|
| 72 | + CURLOPT_SSL_VERIFYHOST => false, |
|
| 73 | + CURLOPT_SSL_VERIFYPEER => false, |
|
| 74 | + CURLOPT_HTTPHEADER => $headers, |
|
| 75 | + ]; |
|
| 76 | + |
|
| 77 | + curl_setopt_array($ch, $curlOpts); |
|
| 78 | + |
|
| 79 | + if (null != $file) { |
|
| 80 | + $cfile = new \CURLFile($file, '', ''); |
|
| 81 | + $body['strFilename']=$cfile; |
|
| 82 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
| 83 | + curl_setopt($ch, CURLOPT_POST, true); |
|
| 84 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|
| 85 | + } else { |
|
| 86 | + switch ($method) { |
|
| 87 | + case 'DELETE': |
|
| 88 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 89 | + break; |
|
| 90 | + case 'DELETEARRAY': |
|
| 91 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 92 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 93 | + break; |
|
| 94 | + case 'PUT': |
|
| 95 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
| 96 | + break; |
|
| 97 | + case 'POST': |
|
| 98 | + if (isset($body)) { |
|
| 99 | + $bodyData = json_encode($body); |
|
| 100 | + if (isset($options['HTTPHEADER'])) { |
|
| 101 | + if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 102 | + $bodyData = $body; |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 107 | + } |
|
| 108 | + break; |
|
| 109 | + case 'ADD': |
|
| 110 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 111 | + break; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if (is_numeric(array_search($method, ['DELETE', 'PUT']))) { |
|
| 115 | + if (isset($body)) { |
|
| 116 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $result = curl_exec($ch); |
|
| 122 | + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 123 | + curl_close($ch); |
|
| 124 | + |
|
| 125 | + $json = null; |
|
| 126 | + if (strpos($result, '<?xml') > -1) { |
|
| 127 | + $xml = simplexml_load_string($result); |
|
| 128 | + $json = self::object2array($xml); |
|
| 129 | + } else { |
|
| 130 | + $json = json_decode($result, true); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if (200 == $code) { |
|
| 134 | + if (isset($json['errors'])) { |
|
| 135 | + throw new ApiError(implode(', ', $json['errors']), $code, $result); |
|
| 136 | + } else { |
|
| 137 | + return $json; |
|
| 138 | + } |
|
| 139 | + } elseif (isset($code) && (!isset($result) || !$result)) { |
|
| 140 | + throw new ApiError('', $code, $result); |
|
| 141 | + } else { |
|
| 142 | + if (isset($json['messages'])) { |
|
| 143 | + $msg = ''; |
|
| 144 | + foreach ($json['messages'] as $key => $value) { |
|
| 145 | + if ($msg !== '') { |
|
| 146 | + $msg .= PHP_EOL; |
|
| 147 | + } |
|
| 148 | + $msg .= $key . ': ' . implode(', ', $value); |
|
| 149 | + } |
|
| 150 | + throw new ApiError($msg, $code, $result); |
|
| 151 | + } elseif (isset($json['errors'])) { |
|
| 152 | + $msg = ''; |
|
| 153 | + foreach ($json['errors'] as $key => $value) { |
|
| 154 | + if ($msg !== '') { |
|
| 155 | + $msg .= PHP_EOL; |
|
| 156 | + } |
|
| 157 | + $msg .= $value; |
|
| 158 | + } |
|
| 159 | + throw new ApiError($msg, $code, $result); |
|
| 160 | + } else { |
|
| 161 | + throw new ApiError($result, $code, $result); |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param $object: JSON object |
|
| 168 | + */ |
|
| 169 | + public static function object2array($object) |
|
| 170 | + { |
|
| 171 | + return @json_decode(@json_encode($object), 1); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Prints on the screen main keys and values of the array. |
|
| 176 | + * |
|
| 177 | + * @param $results: object to be printed on the screen |
|
| 178 | + * @param $deepPrinting: if true, object will be printed recursively |
|
| 179 | + */ |
|
| 180 | + public static function simplePrint($results, $deepPrinting = null) |
|
| 181 | + { |
|
| 182 | + if (isset($results)) { |
|
| 183 | + if (is_array($results)) { |
|
| 184 | + foreach ($results as $key => $result) { |
|
| 185 | + if (is_array($result)) { |
|
| 186 | + foreach ($result as $key1 => $result1) { |
|
| 187 | + if (is_array($result1)) { |
|
| 188 | + if ($deepPrinting) { |
|
| 189 | + echo "<br>$key1 ------><br>"; |
|
| 190 | + self::simplePrint($result1, true); |
|
| 191 | + echo '------<br>'; |
|
| 192 | + } else { |
|
| 193 | + echo $key1.' --> '.'Array() <br>'; |
|
| 194 | + } |
|
| 195 | + } else { |
|
| 196 | + if (is_object($result1)) { |
|
| 197 | + if ($deepPrinting) { |
|
| 198 | + echo "<br>$key1 ------><br>"; |
|
| 199 | + $oarray = (array) $result1; |
|
| 200 | + self::simplePrint($oarray, true); |
|
| 201 | + echo '------<br>'; |
|
| 202 | + } else { |
|
| 203 | + echo $key1.' --> '.'Object <br>'; |
|
| 204 | + } |
|
| 205 | + } else { |
|
| 206 | + if (!is_null($result1)) { |
|
| 207 | + echo $key1.' --> '.$result1.'<br>'; |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + } else { |
|
| 213 | + if (is_object($result)) { |
|
| 214 | + if ($deepPrinting) { |
|
| 215 | + echo "<br>$key ------><br>"; |
|
| 216 | + $oarray = (array) $result; |
|
| 217 | + self::simplePrint($oarray, true); |
|
| 218 | + echo '------<br>'; |
|
| 219 | + } else { |
|
| 220 | + echo $key.' --> '.'Object <br>'; |
|
| 221 | + } |
|
| 222 | + } else { |
|
| 223 | + if (!is_null($result)) { |
|
| 224 | + echo $key.' --> '.$result.'<br>'; |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + //echo "<br>"; |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Generates query or body parameters. |
|
| 236 | + * |
|
| 237 | + * @param $allFields: all known fields could be used for parameters generation |
|
| 238 | + * @param $params: input parameters (array or object) |
|
| 239 | + */ |
|
| 240 | + public static function generateRequestParameters($allFields, $params) |
|
| 241 | + { |
|
| 242 | + $generatedParams = []; |
|
| 243 | + |
|
| 244 | + if (is_array($params)) { |
|
| 245 | + foreach ($allFields as $field) { |
|
| 246 | + if (isset($params[$field])) { |
|
| 247 | + $generatedParams[$field] = $params[$field]; |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + } elseif (is_object($params)) { |
|
| 251 | + foreach ($allFields as $field) { |
|
| 252 | + if (isset($params->{$field})) { |
|
| 253 | + $generatedParams[$field] = $params->{$field}; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + return $generatedParams; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Returns an array of the object properties. |
|
| 263 | + * |
|
| 264 | + * @param $object: An object |
|
| 265 | + * @param $exclude: array of the object parameters to be excluded from the returned array |
|
| 266 | + */ |
|
| 267 | + public static function getObjectProperties($object, $exclude) |
|
| 268 | + { |
|
| 269 | + $objectParameters = []; |
|
| 270 | + |
|
| 271 | + foreach (get_object_vars($object) as $key => $value) { |
|
| 272 | + if (property_exists($object, $key)) { |
|
| 273 | + if (!is_numeric(array_search($key, $exclude))) { |
|
| 274 | + array_push($objectParameters, $key); |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + return $objectParameters; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Returns url path generated from the array of the fields and parameters. |
|
| 284 | + * |
|
| 285 | + * @param $allFields; array of the paossible fields (parameter names) |
|
| 286 | + * @param $params: input parameters (array or object) |
|
| 287 | + */ |
|
| 288 | + public static function generateUrlPath($allFields, $params) |
|
| 289 | + { |
|
| 290 | + $generatedPath = ''; |
|
| 291 | + |
|
| 292 | + if (is_array($params)) { |
|
| 293 | + foreach ($allFields as $field) { |
|
| 294 | + if (isset($params[$field])) { |
|
| 295 | + $generatedPath .= $params[$field].'/'; |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + } elseif (is_object($params)) { |
|
| 299 | + foreach ($allFields as $field) { |
|
| 300 | + if (isset($params->{$field})) { |
|
| 301 | + $generatedPath .= $params->{$field}.'/'; |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + return $generatedPath; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + public static function getFileRealPath($fileName) |
|
| 310 | + { |
|
| 311 | + $rpath = function_exists('curl_file_create') ? curl_file_create(realpath($fileName)) : '@'.realpath($fileName); |
|
| 312 | + |
|
| 313 | + return $rpath; |
|
| 314 | + } |
|
| 315 | 315 | } |
@@ -84,31 +84,31 @@ |
||
| 84 | 84 | curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
| 85 | 85 | } else { |
| 86 | 86 | switch ($method) { |
| 87 | - case 'DELETE': |
|
| 88 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 89 | - break; |
|
| 90 | - case 'DELETEARRAY': |
|
| 91 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 92 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 93 | - break; |
|
| 94 | - case 'PUT': |
|
| 95 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
| 96 | - break; |
|
| 97 | - case 'POST': |
|
| 98 | - if (isset($body)) { |
|
| 99 | - $bodyData = json_encode($body); |
|
| 100 | - if (isset($options['HTTPHEADER'])) { |
|
| 101 | - if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 102 | - $bodyData = $body; |
|
| 103 | - } |
|
| 87 | + case 'DELETE': |
|
| 88 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 89 | + break; |
|
| 90 | + case 'DELETEARRAY': |
|
| 91 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
| 92 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 93 | + break; |
|
| 94 | + case 'PUT': |
|
| 95 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
| 96 | + break; |
|
| 97 | + case 'POST': |
|
| 98 | + if (isset($body)) { |
|
| 99 | + $bodyData = json_encode($body); |
|
| 100 | + if (isset($options['HTTPHEADER'])) { |
|
| 101 | + if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 102 | + $bodyData = $body; |
|
| 104 | 103 | } |
| 105 | - |
|
| 106 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 107 | 104 | } |
| 108 | - break; |
|
| 109 | - case 'ADD': |
|
| 110 | - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 111 | - break; |
|
| 105 | + |
|
| 106 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
|
| 107 | + } |
|
| 108 | + break; |
|
| 109 | + case 'ADD': |
|
| 110 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
|
| 111 | + break; |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | if (is_numeric(array_search($method, ['DELETE', 'PUT']))) { |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | { |
| 35 | 35 | $method = isset($options['method']) ? $options['method'] : 'GET'; |
| 36 | 36 | $query = isset($options['query']) |
| 37 | - ? array_filter($options['query'], function ($x) { |
|
| 37 | + ? array_filter($options['query'], function($x) { |
|
| 38 | 38 | return !is_null($x); |
| 39 | 39 | }) : []; |
| 40 | 40 | |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | $ch = curl_init(); |
| 58 | 58 | |
| 59 | 59 | $url = isset($options['url']) |
| 60 | - ? $options['url'] . '?' . http_build_query(array_merge( |
|
| 60 | + ? $options['url'].'?'.http_build_query(array_merge( |
|
| 61 | 61 | $query, |
| 62 | 62 | ['api_key' => self::getApiKey()] |
| 63 | 63 | )) : ''; |
@@ -76,9 +76,9 @@ discard block |
||
| 76 | 76 | |
| 77 | 77 | curl_setopt_array($ch, $curlOpts); |
| 78 | 78 | |
| 79 | - if (null != $file) { |
|
| 79 | + if (null!=$file) { |
|
| 80 | 80 | $cfile = new \CURLFile($file, '', ''); |
| 81 | - $body['strFilename']=$cfile; |
|
| 81 | + $body['strFilename'] = $cfile; |
|
| 82 | 82 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 83 | 83 | curl_setopt($ch, CURLOPT_POST, true); |
| 84 | 84 | curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | if (isset($body)) { |
| 99 | 99 | $bodyData = json_encode($body); |
| 100 | 100 | if (isset($options['HTTPHEADER'])) { |
| 101 | - if (strpos($options['HTTPHEADER'], 'multipart/form-data') > 0) { |
|
| 101 | + if (strpos($options['HTTPHEADER'], 'multipart/form-data')>0) { |
|
| 102 | 102 | $bodyData = $body; |
| 103 | 103 | } |
| 104 | 104 | } |
@@ -123,14 +123,14 @@ discard block |
||
| 123 | 123 | curl_close($ch); |
| 124 | 124 | |
| 125 | 125 | $json = null; |
| 126 | - if (strpos($result, '<?xml') > -1) { |
|
| 126 | + if (strpos($result, '<?xml')>-1) { |
|
| 127 | 127 | $xml = simplexml_load_string($result); |
| 128 | 128 | $json = self::object2array($xml); |
| 129 | 129 | } else { |
| 130 | 130 | $json = json_decode($result, true); |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - if (200 == $code) { |
|
| 133 | + if (200==$code) { |
|
| 134 | 134 | if (isset($json['errors'])) { |
| 135 | 135 | throw new ApiError(implode(', ', $json['errors']), $code, $result); |
| 136 | 136 | } else { |
@@ -142,16 +142,16 @@ discard block |
||
| 142 | 142 | if (isset($json['messages'])) { |
| 143 | 143 | $msg = ''; |
| 144 | 144 | foreach ($json['messages'] as $key => $value) { |
| 145 | - if ($msg !== '') { |
|
| 145 | + if ($msg!=='') { |
|
| 146 | 146 | $msg .= PHP_EOL; |
| 147 | 147 | } |
| 148 | - $msg .= $key . ': ' . implode(', ', $value); |
|
| 148 | + $msg .= $key.': '.implode(', ', $value); |
|
| 149 | 149 | } |
| 150 | 150 | throw new ApiError($msg, $code, $result); |
| 151 | 151 | } elseif (isset($json['errors'])) { |
| 152 | 152 | $msg = ''; |
| 153 | 153 | foreach ($json['errors'] as $key => $value) { |
| 154 | - if ($msg !== '') { |
|
| 154 | + if ($msg!=='') { |
|
| 155 | 155 | $msg .= PHP_EOL; |
| 156 | 156 | } |
| 157 | 157 | $msg .= $value; |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | if (is_object($result1)) { |
| 197 | 197 | if ($deepPrinting) { |
| 198 | 198 | echo "<br>$key1 ------><br>"; |
| 199 | - $oarray = (array) $result1; |
|
| 199 | + $oarray = (array)$result1; |
|
| 200 | 200 | self::simplePrint($oarray, true); |
| 201 | 201 | echo '------<br>'; |
| 202 | 202 | } else { |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | if (is_object($result)) { |
| 214 | 214 | if ($deepPrinting) { |
| 215 | 215 | echo "<br>$key ------><br>"; |
| 216 | - $oarray = (array) $result; |
|
| 216 | + $oarray = (array)$result; |
|
| 217 | 217 | self::simplePrint($oarray, true); |
| 218 | 218 | echo '------<br>'; |
| 219 | 219 | } else { |