Contents |
The Blesta API system allows local and remote access to the Blesta backend system. API calls are made through POST requests. A POST request consists of a single field-value pair, serialized and base64 encoded.
API calls are made to the API controller located at http://yourdomain.com/blesta_install_dir/api.php.
The basic functionality is as follows:
All responses are returned to the caller in the same format they were sent, or using PHP's serialize format if the received format was not supported.
API access settings can be configured from [Settings] > [API/Cron Settings] > [API Control Options]. All API requests require your Remote Access Key, found in [Settings] > [API/Cron Settings] > [API Control Options].
When performing an API call from PHP it is preferred to use the PHP serialize function, as shown below:
Example using PHP serialize()
$post_data['base64'] = base64_encode(serialize($_POST));
As of version 2.0, Blesta's API also supports the JSON serialization method making it possible to perform API requests from any platform. Below is an example using PHP:
Example using JSON with PHP (requires PHP 5.2.0 or greater)
$post_data['json'] = base64_encode(json_encode($_POST));
The examples below demonstrate the Get Client request using the local access method as well as the remote access method, respectively.
This method is preferred when the caller exists on the same server as Blesta.
<?phpinclude("/path/to/blesta/inc/init.php");
$api = new API();
$data = array();
$data['key'] = "abcdefg1234567"; // Remote Access Key
$data['getclient'] = "true"; // Action to perform
$data['userid'] = "1514"; // Client ID (required for this action)
$postdata = array();
$postdata['base64'] = base64_encode(serialize($data));
$response = unserialize(base64_decode($api->processRequest($postdata['base64'], "base64"));
// NOTE: if using JSON the use the below line instead://$response = unserialize(json_decode($api->processRequest($postdata['json'], "json"));?>This method is required whenever the caller does not reside on the same server as Blesta (i.e. creating an order form on a server other than the one on which Blesta is installed).
<?php// The full URL to your Blesta installation and api.php file.// Use HTTPS whenever possible.$blesta_url = "https://yourdomain.com/api.php";
$data = array();
$data['key'] = "abcdefg1234567"; // Remote Access Key
$data['getclient'] = "true"; // Action to perform
$data['userid'] = "1514"; // Client ID (required for this action)
$postdata = array();
$postdata['base64'] = base64_encode(serialize($data));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $blesta_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
$response = curl_e xec($ch);
curl_close($ch);
$response = unserialize(base64_decode($response));
?>