/** * Specific case for OAuth. Run /oauth.php via your browser to get an access token * * @param Client $client * @param string $code * @param string $oAuthId * @param string $oAuthSecret * * @throws \Exception * * @return mixed */ public static function oauth(Client $client, $code, $oAuthId, $oAuthSecret) { $url = 'https://' . $client->getSubdomain() . '.zendesk.com/oauth/tokens'; $method = 'POST'; $curl = curl_init($url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array('grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $oAuthId, 'client_secret' => $oAuthSecret, 'redirect_uri' => ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 'scope' => 'read'))); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($curl, CURLINFO_HEADER_OUT, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_VERBOSE, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_MAXREDIRS, 3); $response = curl_exec($curl); if ($response === false) { throw new \Exception('No response from curl_exec in ' . __METHOD__); } $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $responseBody = substr($response, $headerSize); $client->setDebug(curl_getinfo($curl, CURLINFO_HEADER_OUT), curl_getinfo($curl, CURLINFO_HTTP_CODE), substr($response, 0, $headerSize)); curl_close($curl); return json_decode($responseBody); }
/** * Specific case for OAuth. Run /oauth.php via your browser to get an access token * * @param Client $client * @param string $code * @param string $oAuthId * @param string $oAuthSecret * * @throws \Exception * * @return mixed */ public static function oauth(Client $client, $code, $oAuthId, $oAuthSecret) { $url = 'https://' . $client->getSubdomain() . '.zendesk.com/oauth/tokens'; $method = 'POST'; $curl = isset(self::$curl) ? self::$curl : new CurlRequest(); $curl->setopt(CURLOPT_URL, $url); $curl->setopt(CURLOPT_POST, true); $curl->setopt(CURLOPT_POSTFIELDS, json_encode(array('grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $oAuthId, 'client_secret' => $oAuthSecret, 'redirect_uri' => ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 'scope' => 'read'))); $curl->setopt(CURLOPT_HTTPHEADER, array('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; }