Code: Select all
<?php
class RESTfulAPI {
function __construct($DomainOrIP, $Key, $Secret) {
$this->BaseUri = $DomainOrIP ."/api/v2/";
$this->ApiKey = $Key;
$this->ApiSecret = $Secret;
// Encription vector initialization
$this->SecretIV = substr(hash("SHA256", $this->ApiKey, true), 0, 16);
}
function base64Url_Encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
// Encription for properties
function APIEncryptData($Data) {
$output = openssl_encrypt($Data, "AES-256-CBC", md5($this->ApiSecret), OPENSSL_RAW_DATA, $this->SecretIV);
return $this->base64Url_Encode($output);
}
function APICall($Endpoint, $Data) {
try {
$Options = array('http'=>
array(
'method'=>'POST',
'header'=>'Content-type: application/x-www-form-urlencoded',
'content'=>'data=' .$this->APIEncryptData($Data)
)
);
$context = stream_context_create($Options);
$RetVal = file_get_contents($this->BaseUri .$Endpoint."/apikey=" .$this->ApiKey, false, $context);
}
Catch (Exception $e) {
$RetVal = '{"warning":"","error":"Generic error"}';
}
return $RetVal;
}
}
//ResellerLevel
//$ApiKey = "";
//$ApiSecret = "";
//admin api key
$ApiKey = "";
$ApiSecret = "";
$API = new RESTfulAPI("https://hotspot.campcomm.net", $ApiKey, $ApiSecret);
//
$Endpoint = 'userWrite';
$Data = '{"id":"0","DomainID":"9","Username":"101","Password":"smith","AddProduct":"1","ProductID":"49"}';
//$Endpoint='userRead';
//$Data = '{"id":2251}';
$JSonRetVal = $API->APICall($Endpoint, $Data);
//if (isset($RetVal["error"]) && $RetVal["error"] != "") {
// echo "Error: " .$RetVal["error"];
//} else {
print_r($JSonRetVal);
?>
{"error":"Error! You have not defined a domain","id":"0"}
If I remove the DomainID keypair I get the same error,
If I use the "reseller" level API key I get
{"error":"Not authorized"}
any helpful hints to get this working, the sample code on the documentation isn't agreeing with me.