<?php
// MyGeotab credentials
$username = "*********";
$password = "*******";
$database = "*******";
// API endpoint
$api_url = "https://my.geotab.com/apiv1";
// Function to perform the API call
function call_api($method, $params, $token = null) {
global $api_url;
$ch = curl_init();
$data = array(
'method' => $method,
'params' => $params
);
if ($token) {
$data['credentials'] = array(
'database' => '*******',
'sessionId' => $token
);
}
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
$response = curl_exec($ch);
if ($response === false) {
die('Curl error: ' . curl_error($ch));
}
curl_close($ch);
return json_decode($response, true);
}
// Authenticate
$response = call_api('Authenticate', array(
'userName' => $username,
'password' => $password,
'database' => $database
));
if (!isset($response['result']['credentials']['sessionId'])) {
die('Authentication failed.');
}
$token = $response['result']['credentials']['sessionId'];
// Get DeviceStatusInfo
$statusInfos = call_api('Get', array(
'typeName' => 'DeviceStatusInfo',
'resultsLimit' => 10
), $token);
if (!isset($statusInfos['result'])) {
die('Failed to get DeviceStatusInfo.');
}
// Extract coordinates
$coordinates = array();
foreach ($statusInfos['result'] as $statusInfo) {
$coordinates[] = array(
'x' => $statusInfo['longitude'],
'y' => $statusInfo['latitude']
);
}
// Get Addresses for coordinates
$addressResults = call_api('GetAddresses', array(
'coordinates' => $coordinates
), $token);
if (!isset($addressResults['result'])) {
die('Failed to get addresses.');
}
// Combine results
$results = array();
for ($i = 0; $i < count($statusInfos['result']); $i++) {
$results[] = array(
'device' => $statusInfos['result'][$i]['device'],
'isDriving' => $statusInfos['result'][$i]['isDriving'],
'address' => $addressResults['result'][$i]
);
}
// Output results
echo '<pre>';
print_r($results);
echo '</pre>';
?>
i am new to this so please help me