<?php
// Define a variable for your private API key
define("APIKEY", "<obtain your API Key from your user Account Settings>");
// Variable to hold the root url to REST server
define("INSTANCE_URL", "https://mywhistlebox.com/api/rest/v1.0/");
// HELPER FUNCTIONS
// Helper function to make an HTTP request call using curl
// supports methods POST, PUT, DELETE and GET
function restCall($method, $url, $apikey, $data=array())
{
$curl = curl_init();
switch (strtoupper($method)) {
case "POST":
curl_setopt($curl, CURLOPT_POST, true);
if (count($data)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if (count($data)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
if (count($data)) {
$url = sprintf("%s?%s", $url, http_build_query($data));
}
break;
default:
if (count($data)) {
$url = sprintf("%s?%s", $url, http_build_query($data));
}
}
// OPTIONS:
// set the request url
curl_setopt($curl, CURLOPT_URL, $url);
// make sure we get a json response
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// add request header with apikey
curl_setopt(
$curl,
CURLOPT_HTTPHEADER,
["Authorization: $apikey"]
);
// EXECUTE the request and check for errors
$result = curl_exec($curl);
if(curl_errno($curl)){
echo 'Request Error:' . curl_error($curl);
}
curl_close($curl);
return $result;
} // end restCall
// OPTIONAL: let's make sure curl extension is installed
if (!function_exists('curl_init')) {
echo "CURL Extension must be installed\n";
exit();
}
// invoke the ping endpoint
// INITIALIZATION: set the request url
$ping_url = INSTANCE_URL."/test/ping";
// ping server to see if connection is established
echo ">>> Pinging...";
$response = restCall('GET', $ping_url, APIKEY);
if ($response !== false) {
// should receive a json response with status='ok'
$result = json_decode($response, true);
if ($result['status'] == 'ok') {
echo "server responded with ok...\n";
} else {
echo "$response...\n";
}
}
echo "DONE!\n";
<?php
// Define a variable for your private API key
define("APIKEY", "<obtain your API Key from your user Account Settings>");
// Variable to hold the root url to REST server
define("INSTANCE_URL", "https://mywhistlebox.com/api/rest/v1.0/");
// HELPER FUNCTIONS
// polyfill of CURLStringFile, available in php >=8.1
// CURLStringFile is useful if you're document comes from a database as a string
// We won't be using this in this example. Included as an alternative technique.
if (!class_exists('CURLStringFile')) {
class CURLStringFile extends CURLFile {
public function __construct(string $file_contents, string $file_name, string $mime = "application/octet-stream")
{
$this->name =
'data://'. $mime .';base64,' . base64_encode($file_contents);
$this->mime = $mime;
$this->postname = $file_name;
}
}
}
// Helper function to make an HTTP request call using curl
// supports methods POST, PUT, DELETE and GET
function restCall($method, $url, $apikey, $data=array())
{
$curl = curl_init();
switch (strtoupper($method)) {
case "POST":
curl_setopt($curl, CURLOPT_POST, true);
if (count($data)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if (count($data)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
if (count($data)) {
$url = sprintf("%s?%s", $url, http_build_query($data));
}
break;
default:
if (count($data)) {
$url = sprintf("%s?%s", $url, http_build_query($data));
}
}
// OPTIONS:
// set the request url
curl_setopt($curl, CURLOPT_URL, $url);
// make sure we get a json response
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// add request header with apikey
curl_setopt(
$curl,
CURLOPT_HTTPHEADER,
["Authorization: $apikey"]
);
// EXECUTE the request and check for errors
$result = curl_exec($curl);
if(curl_errno($curl)){
echo 'Request Error:' . curl_error($curl);
}
curl_close($curl);
return $result;
} // end restCall
// this creates a CURLFIle object for posting
// use if your file upload is a reference to the actual file path
function createCurlFile($filePath) {
$mime = mime_content_type($filePath);
info = pathinfo($filePath);
$name = $info['basename'];
return new CURLFile($filePath, $mime, $name);
}
// this creates a CURLStringFile object for posting
// optionally use if your file upload contains the file contents
function createCurlFileString($contents, $name) {
return new CURLStringFile($contents, $name);
}
// OPTIONAL: let's make sure curl extension is installed
if (!function_exists('curl_init')) {
echo "CURL Extension must be installed.\n";
exit();
}
// INITIALIZATION
// file that you wish to upload (replace with your own test doc)
$upload_file = "api_test_doc.pdf";
// Enter a list of whistle box addresses to upload the above file to
// You should add your own addresses specific to your account (minimum 1)
$wbaddresses = [
"username/box",
];
// REST upload endpoint url
$upload_url = INSTANCE_URL."/user/file/upload";
// We will upload the file to the users account(s) using their WhistleBox Address
foreach ($wbaddresses as $wbaddr) {
$upload_arguments = [
"address" => $wbaddr,
"file" =>createCurlFile($upload_file),
];
echo ">>> Uploading test file $upload_file to $wbaddr...";
$response = restCall('POST', $upload_url, APIKEY, $upload_arguments);
// we should receive a json response with status='ok'
$result = json_decode($response, true);
if ($result['status'] == 'ok') {
echo "ok\n";
} else {
echo "$response\n";
}
}
echo ">>> DONE\n";