diff --git a/src/Analytics/Adapter.php b/src/Analytics/Adapter.php index c340408..ea94ab5 100644 --- a/src/Analytics/Adapter.php +++ b/src/Analytics/Adapter.php @@ -136,56 +136,52 @@ public function call(string $method, string $path = '', array $headers = [], arr unset($headers[$i]); } - try { - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_USERAGENT, php_uname('s').'-'.php_uname('r').':php-'.phpversion()); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) { - $len = strlen($header); - $header = explode(':', strtolower($header), 2); - - if (count($header) < 2) { // ignore invalid headers - return $len; - } - - $responseHeaders[strtolower(trim($header[0]))] = trim($header[1]); - + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_USERAGENT, php_uname('s').'-'.php_uname('r').':php-'.phpversion()); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) { + $len = strlen($header); + $header = explode(':', strtolower($header), 2); + + if (count($header) < 2) { // ignore invalid headers return $len; - }); - - if ($method != 'GET') { - curl_setopt($ch, CURLOPT_POSTFIELDS, $query); } - $responseBody = curl_exec($ch); + $responseHeaders[strtolower(trim($header[0]))] = trim($header[1]); - $responseType = $responseHeaders['Content-Type'] ?? ''; - $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); + return $len; + }); - switch (substr($responseType, 0, strpos($responseType, ';'))) { - case 'application/json': - $responseBody = json_decode($responseBody, true); - break; - } + if ($method != 'GET') { + curl_setopt($ch, CURLOPT_POSTFIELDS, $query); + } - if (curl_errno($ch)) { - throw new Exception(curl_error($ch), $responseStatus); - } + $responseBody = curl_exec($ch); - if ($responseStatus >= 400) { - if (is_array($responseBody)) { - throw new Exception(json_encode($responseBody), $responseStatus); - } else { - throw new Exception($responseStatus.': '.$responseBody, $responseStatus); - } - } + $responseType = $responseHeaders['Content-Type'] ?? ''; + $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); - return $responseBody; - } finally { - curl_close($ch); + switch (substr($responseType, 0, strpos($responseType, ';'))) { + case 'application/json': + $responseBody = json_decode($responseBody, true); + break; } + + if (curl_errno($ch)) { + throw new Exception(curl_error($ch), $responseStatus); + } + + if ($responseStatus >= 400) { + if (is_array($responseBody)) { + throw new Exception(json_encode($responseBody), $responseStatus); + } else { + throw new Exception($responseStatus.': '.$responseBody, $responseStatus); + } + } + + return $responseBody; } /**