Extend License Plate Recognizer module with usage info endpoint and enhanced API handling
- Added `get_usage` method in `licenseplaterecognizer` to retrieve module usage statistics from the API. - Introduced `licenseplaterecognizer_info` class to parse and manage API response data. - Enhanced API request handling with updated default `api_url` and improved error handling. - Refined cURL implementation for consistent API interaction.
This commit is contained in:
@@ -5,6 +5,7 @@ require_once WD . '/modules/licenseplaterecognizer/licenseplaterecognizer_c.php'
|
||||
|
||||
use Exception;
|
||||
use interfaces\licenseplaterecognizer_i;
|
||||
use licenseplaterecognizer\helpers\licenseplaterecognizer_info;
|
||||
use licenseplaterecognizer\licenseplaterecognizer_c;
|
||||
|
||||
class licenseplaterecognizer implements licenseplaterecognizer_i
|
||||
@@ -18,7 +19,7 @@ class licenseplaterecognizer implements licenseplaterecognizer_i
|
||||
* API URL
|
||||
* @var string
|
||||
*/
|
||||
private string $api_url = 'https://api.platerecognizer.com/v1/plate-reader/';
|
||||
private string $api_url = 'https://vs4sws0kg4sog4ssw8kwowk4.coolify.truckwash.dk'; // Default (cloud): 'https://api.platerecognizer.com'; (without /v1/plate-reader/)';
|
||||
|
||||
|
||||
public function __construct()
|
||||
@@ -49,11 +50,12 @@ class licenseplaterecognizer implements licenseplaterecognizer_i
|
||||
//ADD PARAMETER IN REQUEST LIKE regions
|
||||
$data = array(
|
||||
'upload' => $base64_image,
|
||||
'regions' => 'us-ca' // Optional
|
||||
//'regions' => 'dk' // Optional
|
||||
);
|
||||
|
||||
// Prepare new cURL resource
|
||||
$ch = curl_init('https://api.platerecognizer.com/v1/plate-reader/');
|
||||
//$ch = curl_init('https://api.platerecognizer.com/v1/plate-reader/');
|
||||
$ch = curl_init($this->api_url . '/v1/plate-reader/');
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
@@ -69,6 +71,10 @@ class licenseplaterecognizer implements licenseplaterecognizer_i
|
||||
// Submit the POST request and close cURL session handle
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
// Print the response from the server
|
||||
if ($result === false) {
|
||||
throw new Exception('Error in API request.');
|
||||
}
|
||||
|
||||
$response_data = json_decode($result, true);
|
||||
if (isset($response_data['results']) && count($response_data['results']) > 0) {
|
||||
@@ -86,4 +92,36 @@ class licenseplaterecognizer implements licenseplaterecognizer_i
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws Exception If the module is not enabled or if there is an error in the API request
|
||||
* @throws Exception If the API response cannot be parsed
|
||||
*/
|
||||
public function get_usage(): licenseplaterecognizer_info
|
||||
{
|
||||
// Require the module to be enabled
|
||||
$this->requireModuleEnabled();
|
||||
$curl = curl_init();
|
||||
curl_setopt_array($curl, array(
|
||||
CURLOPT_URL => $this->api_url . '/info/',
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_ENCODING => '',
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_TIMEOUT => 0,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2TLS,
|
||||
CURLOPT_CUSTOMREQUEST => 'GET',
|
||||
CURLOPT_HTTPHEADER => array(
|
||||
'Authorization: Token ' . $this->config->api_key->getVariableValue()
|
||||
),
|
||||
));
|
||||
$response = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
$response_data = json_decode($response, true);
|
||||
if ($response_data === null) {
|
||||
throw new Exception('Error parsing API response.');
|
||||
}
|
||||
return new licenseplaterecognizer_info($response_data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user