Page 1 of 1

API Help

Posted: 06 May 2022, 20:37
by tylerlane
Hello

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); ?>
I'm getting the following error.
{"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.

Re: API Help

Posted: 06 May 2022, 22:26
by tylerlane

Code: Select all

$Data = '{"id":"0","DomainID":"5","UserName":"Cabin101","Password":"smith","AddProduct":"1","ProductID":"49"}';

Had a few errors in the data line.

Wrong DomainId and UserName I had typed as Username

Re: API Help

Posted: 09 May 2022, 08:37
by HSNMSupport
Hi Tyler,
what is the purpose of your code?
do you need to add a user or a product to a user?