/**
* Specific case for OAuth. Run /oauth.php via your browser to get an access token
*
* @param HttpClient $client
* @param string $code
* @param string $oAuthId
* @param string $oAuthSecret
*
* @throws \Exception
* @return mixed
*/
public static function oauth(HttpClient $client, $code, $oAuthId, $oAuthSecret)
{
$url = 'https://' . $client->getSubdomain() . '.zendesk.com/oauth/tokens';
$protocol = $_SERVER['HTTPS'] ? 'https://' : 'http://';
$curl = isset(self::$curl) ? self::$curl : new CurlRequest();
$curl->setopt(CURLOPT_URL, $url);
$curl->setopt(CURLOPT_POST, true);
$curl->setopt(CURLOPT_POSTFIELDS, json_encode(['grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $oAuthId, 'client_secret' => $oAuthSecret, 'redirect_uri' => $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 'scope' => 'read']));
$curl->setopt(CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$curl->setopt(CURLINFO_HEADER_OUT, true);
$curl->setopt(CURLOPT_RETURNTRANSFER, true);
$curl->setopt(CURLOPT_CONNECTTIMEOUT, 30);
$curl->setopt(CURLOPT_TIMEOUT, 30);
$curl->setopt(CURLOPT_SSL_VERIFYPEER, false);
$curl->setopt(CURLOPT_HEADER, true);
$curl->setopt(CURLOPT_VERBOSE, true);
$curl->setopt(CURLOPT_FOLLOWLOCATION, true);
$curl->setopt(CURLOPT_MAXREDIRS, 3);
$response = $curl->exec();
if ($response === false) {
throw new \Exception(sprintf('Curl error message: "%s" in %s', $curl->error(), __METHOD__));
}
$headerSize = $curl->getinfo(CURLINFO_HEADER_SIZE);
$responseBody = substr($response, $headerSize);
$responseObject = json_decode($responseBody);
$client->setDebug($curl->getinfo(CURLINFO_HEADER_OUT), $curl->getinfo(CURLINFO_HTTP_CODE), substr($response, 0, $headerSize), isset($responseObject->error) ? $responseObject : null);
$curl->close();
self::$curl = null;
return $responseObject;
}