Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 37 additions & 41 deletions src/Analytics/Adapter.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
}

/**
Expand Down
Loading