<?php
/*
** Simple example of an API Client.
** This example of built on top of Zend_Http_Client which handles general request and response data.
** Similar libraries may be used.
*/
class ApiClient {
/**
* @param string $method
* @param string $uri
* @param array $config
* @param string $postPutData
* @param array $getData
* @param bool $autoRenew
*
* @return Zend_Http_Response
* @throws ApiClient_Exception
* @throws Zend_Http_Client_Exception
*/
public static function execute($method, $uri, $config, $postPutData='', $getData=array(), $autoRenew=true, $contentIsJson=true) {
if (!isset($_SESSION['oauth2_tokendata'])) {
self::authorize($config);
}
$httpClient = new Zend_Http_Client();
$httpClient->setHeaders('Authorization', 'Bearer ' . $_SESSION['oauth2_tokendata']['access_token']);
$httpClient->setHeaders('Accept', 'application/vnd.api+json');
$httpClient->setMethod($method);
$httpClient->setUri($uri);
if (isset($config['cookies'])) {
foreach ($config['cookies'] as $name => $value) {
$httpClient->setCookie($name, $value);
}
}
if (!empty($postPutData)) {
if ($contentIsJson === true) {
$httpClient->setHeaders('Content-Type', 'application/vnd.api+json');
}
$httpClient->setRawData($postPutData);
}
if (!empty($getData)) {
$httpClient->setParameterGet($getData);
}
if (($method == 'POST') || ($method == 'PUT')) {
$httpClient->setEncType($httpClient::ENC_FORMDATA);
}
$response = $httpClient->request();
if ($autoRenew === true) {
$status = $response->getStatus();
if ($status == 401) {
$json = @json_decode($response->getBody(), true);
if (is_array($json) && isset($json['error']) && $json['error'] == 'expired_token') {
// renew the token and try again
self::tokenRefreshToken($config, $_SESSION['oauth2_tokendata']['refresh_token']);
return self::execute($method, $uri, $config, $postPutData, $getData, $autoRenew=false);
}
}
}
return array(
'request' => $httpClient->getLastRequest(),
'response' => $response->getHeadersAsString() . "\n" . $response->getBody(),
'responseObj' => $response
);
}
/**
* @param $config
*/
public static function authorize($config) {
$url = $config['authorize_endpoint'];
$url .= ((strpos($url, '?') === false) ? '?' : '&') . 'response_type=code';
$url .= '&client_id=' . urlencode($config['client_id']);
if (!empty($config['redirect_uri'])) {
$url .= '&redirect_uri=' . urlencode($config['redirect_uri']);
}
if (!empty($config['state'])) {
$url .= '&status=' . urlencode($config['state']);
}
if (!empty($config['scope'])) {
if (is_array($config['scope'])) {
$config['scope'] = implode(' ', $config['scope']);
}
$url .= '&scope=' . urlencode($config['scope']);
}
header('Location: ' . $url);
exit;
}
/**
* @param $config
* @param $authorizationCode
*
* @return mixed
* @throws ApiClient_Exception
* @throws Zend_Http_Client_Exception
*/
public static function tokenAuthorizationCode($config, $authorizationCode) {
$url = $config['token_endpoint'];
$url .= ((strpos($url, '?') === false) ? '?' : '&') . 'grant_type=authorization_code';
$url .= '&code=' . urlencode($authorizationCode);
if (!empty($config['redirect_uri'])) {
$url .= '&redirect_uri=' . urlencode($config['redirect_uri']);
}
$httpClient = new Zend_Http_Client();
$httpClient->setAuth($config['client_id'], $config['client_secret']);
$httpClient->setUri($url);
if (isset($config['cookies'])) {
foreach ($config['cookies'] as $name => $value) {
$httpClient->setCookie($name, $value);
}
}
$response = $httpClient->request('POST');
if ($response->getStatus() == 200) {
$_SESSION['actionstep_oauth2_tokendata'] = json_decode($response->getBody(), true);
return json_decode($response->getBody(), true);
}
throw new ApiClient_Exception($httpClient);
}
/**
* @param $config
* @param $refreshToken
*
* @return mixed
* @throws ApiClient_Exception
* @throws Zend_Http_Client_Exception
*/
public static function tokenRefreshToken($config, $refreshToken) {
$url = $config['token_endpoint'];
$url .= ((strpos($url, '?') === false) ? '?' : '&') . 'grant_type=refresh_token';
$url .= '&refresh_token=' . urlencode($refreshToken);
$httpClient = new Zend_Http_Client();
$httpClient->setAuth($config['client_id'], $config['client_secret']);
$httpClient->setUri($url);
if (isset($config['cookies'])) {
foreach ($config['cookies'] as $name => $value) {
$httpClient->setCookie($name, $value);
}
}
$response = $httpClient->request('POST');
if ($response->getStatus() == 200) {
$_SESSION['oauth2_tokendata'] = json_decode($response->getBody(), true);
return json_decode($response->getBody(), true);
}
throw new ApiClient_Exception($httpClient);
}
}
|