/
PHP Zend Framework
PHP Zend Framework
Ted Jordan (Unlicensed)
Owned by Ted Jordan (Unlicensed)
Connect to API Expand source
<?php $this->_config = array( 'authorize_endpoint'=> 'https://api.actionstep.com/api/oauth/authorize', 'token_endpoint' => 'https://api.actionstep.com/api/oauth/token', 'client_id' => {YOUR CLIENT_ID}, 'client_secret' => {YOUR CLIENT_SECRET}, 'redirect_uri' => {URI YOU WANT ACTIONSTEP TO REDIRECT THE USER TO ONCE AUTHORIZED}, 'scope' => {THE SCOPE YOU ARE REQUESTING ACCESS TO (COMMA SEPARATED LIST OF RESOURCES - i.e. Actions,Participants,Timerecords)}, 'cookies' => array() ); ApiClient::authorize($this->_config);
Get an Access Token Expand source
<?php // Convert to Access Token $response = ApiClient::tokenAuthorizationCode($this->_config, $_GET['code']); // Store the Access Token $_SESSION['oauth2_tokendata'] = $response; // Redirect to application to execute API requests $this->redirect({URI TO YOUR APPLICATION});
Send/Receive Data Expand source
<?php $method = {HTTP METHOD: GET,POST,PUT,DELETE,OPTIONS}; $uri = {REQUEST TO API RESOURCE - i.e. https://api.actionstep.com/api/rest/actions/1} $postData = {IF POST METHOD THEN ARRAY OF DATA, OTHERWISE NULL} $getData = {IF GET METHOD THEN ARRAY of DATA, OTHERWISE NULL} // Execute request $result = ApiClient::execute($method, $uri, $this->_config, $postData, $getData); // The $result is going to contain request and response data $result['request'] = ... $result['response'] = ...
API Client Class Expand source
<?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); } }
Related content
API Quick Start Guide
API Quick Start Guide
More like this
Examples of Authorisation and use of the Actionstep API in the staging environment using Postman
Examples of Authorisation and use of the Actionstep API in the staging environment using Postman
More like this
PHP SDK example
PHP SDK example
More like this
PhoneGap jQuery Mobile
PhoneGap jQuery Mobile
Read with this