Connecting to API via PHP #906
Replies: 2 comments
-
This automatically generated reply acts as a friendly reminder. Answers to your questions will most often come from the community, from developers like yourself. You will, from time to time, find that Axis employees answers some of the questions, but this is not a guarantee. Think of the discussion forum as a complement to other support channels, not a replacement to any of them. If your question remains unanswered for a period of time, please revisit it to see whether it can be improved by following the guidelines listed in Axis support guidelines. |
Beta Was this translation helpful? Give feedback.
-
Hi @webguy2022 , <?php
$server = "http://10.176.12.49//local/objectanalytics/control.cgi";
$username = "VLTuser";
$password = "XXXXXX";
$scenario_uid = 1697489680; // Replace with actual scenario ID
// Create the JSON request body
$data = json_encode([
"apiVersion" => "1.0",
"method" => "getAccumulatedCounts",
"params" => ["scenario" => $scenario_uid]
]);
// Initialize a cURL session
$ch = curl_init($server);
// Set cURL options for Digest authentication and POST request
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json"
]);
// Execute the request
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($response === false) {
echo "cURL Error: " . curl_error($ch);
} else {
echo "HTTP Code: $http_code\n";
echo "Response: $response\n";
}
// Close cURL session
curl_close($ch);
?> Output: |
Beta Was this translation helpful? Give feedback.
-
I'm trying to create an API connection to get People Counter stats. I believe there is an issue with our certificate which is causing an error, but before I ask our sysadmin, I want to ensure I am attempting to authenticate correctly using PHP and curl. I haven't found much documentation on this from Axis.
This is what I have now. If there's something wrong with this method, can you please advise on how to enable the secure connection?
`
$host = "https://path-to-server";
$path = "/path-to-api-export/";
$headers['Host'] = $host;
$headers['Content-Type'] = "application/x-www-form-urlencoded";
$data = array(
'grant_type' => 'client_credentials',
'client_id' => $client_id,
'client_secret' => $client_secret
);
$timeout = 3600;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host . $path);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$auth = curl_exec( $ch );`
Beta Was this translation helpful? Give feedback.
All reactions