diff --git a/src/Analytics/Adapter.php b/src/Analytics/Adapter.php index ef035a3..c340408 100644 --- a/src/Analytics/Adapter.php +++ b/src/Analytics/Adapter.php @@ -136,54 +136,56 @@ public function call(string $method, string $path = '', array $headers = [], arr unset($headers[$i]); } - 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]); - - return $len; - }); + 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]); - if ($method != 'GET') { - curl_setopt($ch, CURLOPT_POSTFIELDS, $query); - } + return $len; + }); - $responseBody = curl_exec($ch); + if ($method != 'GET') { + curl_setopt($ch, CURLOPT_POSTFIELDS, $query); + } - $responseType = $responseHeaders['Content-Type'] ?? ''; - $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $responseBody = curl_exec($ch); - switch (substr($responseType, 0, strpos($responseType, ';'))) { - case 'application/json': - $responseBody = json_decode($responseBody, true); - break; - } + $responseType = $responseHeaders['Content-Type'] ?? ''; + $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); - if (curl_errno($ch)) { - throw new \Exception(curl_error($ch), $responseStatus); - } + switch (substr($responseType, 0, strpos($responseType, ';'))) { + case 'application/json': + $responseBody = json_decode($responseBody, true); + break; + } - curl_close($ch); + 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); + if ($responseStatus >= 400) { + if (is_array($responseBody)) { + throw new Exception(json_encode($responseBody), $responseStatus); + } else { + throw new Exception($responseStatus.': '.$responseBody, $responseStatus); + } } - } - return $responseBody; + return $responseBody; + } finally { + curl_close($ch); + } } /**