Skip to content

Instantly share code, notes, and snippets.

@hanicker
Created April 21, 2022 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanicker/07ac019a0da8691a8b07a7d1d2ca4578 to your computer and use it in GitHub Desktop.
Save hanicker/07ac019a0da8691a8b07a7d1d2ca4578 to your computer and use it in GitHub Desktop.
<?php
class TargheApi
{
private $token;
const ENDPOINT = "https://informazioni-targhe.p.rapidapi.com";
public function __construct($token)
{
$this->token = $token;
}
public function submit($targhe, $op)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => self::ENDPOINT . "/job/submit",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER => [
"X-RapidAPI-Host: informazioni-targhe.p.rapidapi.com",
"X-RapidAPI-Key: ".$this->token,
"content-type: application/json"
],
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
"targhe" => $targhe,
"op" => $op,
]),
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$data = json_decode($response);
return $data->job_id;
}
}
public function getStatus($job)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => self::ENDPOINT . "/job/status?job=" . $job,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER => [
"X-RapidAPI-Host: informazioni-targhe.p.rapidapi.com",
"X-RapidAPI-Key: ".$this->token,
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$data = json_decode($response);
return $data;
}
}
public function waitCompleted($job)
{
while (true) {
$status = $this->getStatus($job);
if ($status->completed) {
break;
}
sleep(10);
}
}
public function retrieve($job)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => self::ENDPOINT . "/job/retrieve?job=" . $job,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER => [
"X-RapidAPI-Host: informazioni-targhe.p.rapidapi.com",
"X-RapidAPI-Key: ".$this->token,
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$data = json_decode($response);
return $data;
}
}
}
// TEST
$targhe = [
"CN161TB",
"CN162TB"
];
$op = "rca";
$key = "CHIAVERAPIDAPI"; // chiave RapidAPI
$targheApi = new TargheApi($key);
$job = $targheApi->submit($targhe, $op);
$targheApi->waitCompleted($job);
$result = $targheApi->retrieve($job);
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment