Standalone mode
TorneAPI is a standalone library, pluggable with TorneLIB, so working with the API can be one in two ways. Either from TorneLIB or in a standalone environment. What you need, to make this work in standalone mode with TorneAPI as the only available APi, is the following files:
- classes/torneapi_client.php
- classes/torneapi_core.php
- classes/tornevall_network.php
- libraries/lib_torneapi.php
- autoload.php
The more files you keep, from the repo, that is normally located in libraries, the more API's will also become available. For example, including the files for Facebook, will make the Facebook API available.
When the files are loaded, you can start working with the API. For TorneAPI it's quite simple. You need to know what verb you want to use and send it through the API engine:
<?php
require_once("autoload.php");
$API = new \TorneLIB\TorneAPI();
try {
$TAPI = $API->Intialize("Tornevall");
if ($API->getLoadedLibraries("Tornevall")) {
echo $TAPI->Get("test")->version;
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
However, there are also subcommands under some of the API verb suite, for example in our DNSBLv5: About and usage-API where you can request information about IP-addresses like this:
<?php
require_once("autoload.php");
$API = new \TorneLIB\TorneAPI();
try {
$TAPI = $API->Intialize("Tornevall");
if ($API->getLoadedLibraries("Tornevall")) {
var_dump($TAPI->Post("dnsbl/ip", array("bulk"=>"255.255.255.255")));
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
{
"ipResponse": {
"255.255.255.255": {
"blacklist": {
"ip": "255.255.255.255",
"typebit": "64",
"typestring": "mockdata",
"discovered": "2016-10-20 16:07:18",
"exitnode": "",
"tornode": "0",
"deleted": "0000-00-00 00:00:00",
"lastdelete": "0000-00-00 00:00:00"
},
"removed": []
}
}
}
In this request, we are sending a request to the dnsbl interface, with a question if an ip exists. As the subfunction for this is "ip", we'll call dnsbl/ip. In the DNSBL, it is also post a bulk with ip-addresses, to get information about multiple hosts in the same time, so a API post may also look like this:
<?php
require_once("autoload.php");
$API = new \TorneLIB\TorneAPI();
try {
$TAPI = $API->Intialize("Tornevall");
if ($API->getLoadedLibraries("Tornevall")) {
//echo $TAPI->Get("test")->version;
var_dump($TAPI->Post("dnsbl/ip", array("bulk"=>array("255.255.255.255", "255.255.255.254"))));
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
{
"ipResponse": {
"255.255.255.255": {
"blacklist": {
"ip": "255.255.255.255",
"typebit": "64",
"typestring": "mockdata",
"discovered": "2016-10-20 16:07:18",
"exitnode": "",
"tornode": "0",
"deleted": "0000-00-00 00:00:00",
"lastdelete": "0000-00-00 00:00:00"
},
"removed": []
},
"255.255.255.254": {
"blacklist": {
"ip": "255.255.255.254",
"typebit": "64",
"typestring": "mockdata",
"discovered": "2016-07-02 10:07:47",
"exitnode": "",
"tornode": "0",
"deleted": "0000-00-00 00:00:00",
"lastdelete": "0000-00-00 00:00:00"
},
"removed": []
}
}
}
Running from TorneLIB
Running from TorneLIB has an advantage over the stand alone method - running this way, all lib files not only the API will become available, so running standalone is primary used when this is not needed. It however works in a similar way. In this example, we are initializing the API through TorneLIB, and all communication are handled through a single function. As there's also authentication involved here, this might not always be the most preferred way to run.
$LibDir = "/usr/local/TorneLIB/";
if (!file_exists($LibDir . "autoload.php")) { die("TorneLIB missing."); }
require_once($LibDir . "/autoload.php");
/** @var $Tornevall Initialize TorneLIB */
$Tornevall = new \TorneLIB\TorneLIB();
/**
* API Communication
*
* @param string $method
* @param array $PostData
*/
function API($method = "", $PostData = array())
{
// Setting a global here, since TorneLIB are used elsewhere too
global $Tornevall;
$API = $Tornevall->API->Intialize("Tornevall");
// Build a standard output array, as this is implemented as a gateway on the server, that responds with JSON via Ajax calls
$getApi = array(
'success' => false,
'faultstring' => null
);
try {
// Using Post only.
$getApi['response'] = $API->Post($method, $PostData);
$getApi['success'] = true;
} catch (Exception $e) {
// The API throws exceptions so we can catch any occured errors here
$getApi['faultstring'] = $e->getMessage();
}
$APIResponse = array(
'api' => $getApi
);
header("Content-Type: application/json");
die(json_encode($APIResponse));
}
var_dump(API("test"));