Skip to content

Instantly share code, notes, and snippets.

@mariusvw
Forked from tvisser/Afas.class.php
Created August 3, 2017 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariusvw/802bce5afde317d38bd649c3f16580ea to your computer and use it in GitHub Desktop.
Save mariusvw/802bce5afde317d38bd649c3f16580ea to your computer and use it in GitHub Desktop.
PHP Class for easily using the Afas GetConnector or UpdateConnector
<?php
/**
* Class Afas
*
* This class has 3 static functions for accessing data using the Afas GetConnectors and UpdateConnectors.
* It includes the GET, POST and PUT request in their valid format.
*
* IMPORTANT: For it to work, remember to change the BASE_URL and TOKEN constants in the class.
*
* At the bottom of this file you'll find several example usages.
* Keep in mind that these functions are static, so use the Afas::GET($_connector_name) for example.
*
* @author Thoby Visser <visser.thoby@hotmail.com>
* @copyright Copyright (c) 2017
*/
Class Afas {
/**
* Change the BASE_URL and TOKEN below.
* The BASE_URL can be obtained by replacing 00000 with your environment ID.
* The TOKEN can be obtained by going to the BASE_URL and logging into your connector.
*/
const BASE_URL = 'https://00000.afasonlineconnector.nl/profitrestservices';
const TOKEN = '<token><version>1</version><data>TOKENSTRING - Retrieved from above URL</data></token>';
/**
* Enable or disable error-debugging by changing the following variable.
* The error messages will be displayed in a preformatted text block.
*/
const DEBUG_ERRORS = true;
/**
* Sends GET request to the provided Afas-Connector.
* Will return object if data is found, will return FALSE if an error occurred.
*
* @param string $_connector_name Name of the Afas GetConnector.
* @param array $_parameters URL parameters for querying data.
* @return object/bool Returns retrieved Afas data. If an error occurred; returns false.
*/
public static function GET($_connector_name, $_parameters = [])
{
return Afas::REQUEST('GET', $_connector_name, $_parameters);
}
/**
* Sends POST request to the provided Afas-Connector.
* Given $_content will be send in the post-content. Given $_content can either be an array or json string.
* Will return object if data is found, will return FALSE if an error occurred.
*
* @param string $_connector_name Name of the Afas UpdateConnector.
* @param array/string $_content String/array as post-content. When providing an array it will be json encoded.
* @return bool Returns TRUE if execution was successful.
*/
public static function POST($_connector_name, $_content)
{
return Afas::REQUEST('POST', $_connector_name, [], $_content);
}
/**
* Sends PUT request to the provided Afas-Connector.
* Given $_content will be send in the post-content. Given $_content can either be an array or json string.
* Will return object if data is found, will return FALSE if an error occurred.
*
* @param string $_connector_name Name of the Afas UpdateConnector.
* @param array/string $_content String/array as post-content. When providing an array it will be json encoded.
* @return bool Returns TRUE if execution was successful.
*/
public static function PUT($_connector_name, $_content)
{
return Afas::REQUEST('PUT', $_connector_name, [], $_content);
}
/**
* Private function that serves as the base for all Update- and GetConnector requests.
*
* @param string $_request_type Request-typing for the different functions.
* @param string $_connector_name Name of the Afas Connector.
* @param array $_parameters URL parameters for querying data.
* @param array/string $_content String/array as post-content. When providing an array it will be json encoded.
* @return object/bool Returns FALSE if execution failed, return TRUE on a successful execution
* and returns an object on GET-requests.
*/
private static function REQUEST($_request_type, $_connector_name, $_parameters = [], $_content = [])
{
# Build a query string for the provided parameters
$query_string = (!empty($_parameters)) ? '?' . http_build_query($_parameters) : '';
# Serialize given array (if an array was given)
$json_content = (is_array($_content)) ? json_encode($_content) : $_content;
# Request data by given url and parameters
curl_setopt_array($ob_curl = curl_init(), array(
CURLOPT_URL => Afas::BASE_URL . '/connectors/' . $_connector_name . $query_string,
CURLOPT_CUSTOMREQUEST => $_request_type,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => (!empty($json_content)),
CURLOPT_POSTFIELDS => $json_content,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json_content),
'Authorization: AfasToken ' . base64_encode(Afas::TOKEN),
)
));
# Execute cURL object and decode retrieved JSON data
if($result = curl_exec($ob_curl)) $data = json_decode($result);
# Check if executed cURL object returned errors.
if(curl_error($ob_curl)) {
echo Afas::DEBUG_ERRORS ? "<pre>A cURL error occurred: \r\n" . curl_error($ob_curl) . "\r\n</pre>" : "";
} else {
# Check if JSON is decoded successfully
if(json_last_error() != JSON_ERROR_NONE) {
echo Afas::DEBUG_ERRORS ? "<pre>A JSON error occurred.\r\n</pre>" : "";
} else {
# Check if data is set and is an object
if(isset($data)) {
# If Afas returns a ProfitLogReference, catch error.
if(isset($data->profitLogReference)){
echo Afas::DEBUG_ERRORS ? "<pre>An Afas error occurred. \r\nProfit Log ID: $data->profitLogReference</pre>" : "";
} else
# If Afas returns a message (usually when using the UpdateConnector)
if(isset($data->message)){
echo Afas::DEBUG_ERRORS ? "<pre>Afas returned a message: \r\n$data->message</pre>" : "";
} else if(isset($data->rows) && is_array($data->rows)) return $data->rows;
} else if(is_string($result)) {
# If no data returned but request was successfully executed return true
# (usually on a successful execution of the UpdateConnectors)
return true;
}
}
}
# If an error occurred, return false.
return false;
}
}
### Example usages of a GET request:
# Requests data using the Profit_Projects GetConnector.
# Takes 10 rows, and orders by ProjectId descending.
$profit_projects = Afas::GET('Profit_Projects', [
'orderbyfieldids' => '-ProjectId',
'take' => 10
]);
# Printing out retrieved data:
echo '<pre>';
print_r($profit_projects);
echo '</pre>';
### Example usages of a POST request:
# Posts data to the KnSubjectWorkflowReaction UpdateConnector.
# Posts simple comment to subject with Id 2300.
$comment_text = "Updated by Thoby on " . date("Y-m-d");
$new_workflow_comment = [
'KnWorkflow' => [
'Element' => [
'Fields' => [
'SbId' => 2300, # Subject-Id
'Tx' => $comment_text # Comment
]
]
]
];
if(Afas::POST('KnSubjectWorkflowReaction', $new_workflow_comment)) {
echo 'Data was successfully posted!';
} else {
echo 'Execution failed.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment