EDIT: I have included the documentation link for this API. The link is:
https://pinfo.hikvision.com/hkwsen/unzip/20240201150721_74009_doc/
So I have been trying to call the parkingfee/calculate API using cURL PHP and I am able to get the 200 status code which means it reaches the server. But somehow the JSON data is not showing up even though I get the 200 status code. I have concluded it’s an authentication issue but I couldn’t really figure out where I went wrong. I have contacted the tech support from hikvision but they would not help in that term and kept on suggesting for me to use CSHCSDKDemo application that comes with the HCP download. It seems they couldn’t understand my request. The image I attached is the error I have been getting and it shows the status code 200 as well. Any help would be appreciated, thank you.
At first, I kept getting errors like HTTP failed request: 400 status code but when I changed the headers parameters, I finally got the 200 status code which means successfully called the API but the JSON data that comes with it does not show up. And also, in the documentation, it mentioned that I need to calculate the signature using So I am not sure where I went wrong.
Below is the instructions on how to calculate the signature:
Signature Calculation
- Calculate the signature string by HmacSHA256 algorithm with the appKey or appSecret to
generate message digest. - Calculate the message digest by BASE64 algorithm to generate the signature.
This is my code that I use to calculate the signature:
$stringToSign = "{$httpMethod}n". "{$contentMD5}n". "{$contentType}n". "{$apiKey}n". "{$x_ca_nonce}n". "{$timestampInMilliseconds}n". "{$urlEndpoint}n";
// Generate Signature
$signature = hash_hmac('sha256', $stringToSign, $apiSecret, true);
// Encode Signature (base64)
$base64encodedSignature = base64_encode($signature);
Below is the header parameter that gave the 200 status code.
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_1');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POST, 1); //set request method to post
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Basic', // Adjust according to API's authentication requirements
'accept: '. $accept.,
'accept-encoding: '. $acceptEncoding,
'accept-language: '. $acceptLanguage,
'connection: '. $connection,
'content-length: '. $contentLength, //$contentLength,
'content-md5: '. $contentMD5,
'content-type: '. $contentType,
'host: '. $host,
'method: '. $httpMethod,
'server: '. $server,
'transferEncoding: '. $transferEncoding,
'x-ca-key: ' . $apiKey,
'x-ca-signature: ' . $base64encodedSignature,
'x-ca-signature: x-ca-key, x-ca-timestamp',
'x-ca-timestamp: ' . $timestampInMilliseconds,
'x-ca-nonce: ' . $x_ca_nonce,
'x-ca-secret:' . $apiSecret,
'x-requested-with: '. $x_requested_with,
'url: ' . $apiUrl
]);
I am not sure if the error comes from the signature calculation or the header. But the error I’m getting shows:
Status Code: 200 Error decoding JSON response: Control character error, possibly incorrectly encoded
abraham is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.