Skip to main content

How to run Geotab api script on sqlserver

MBost-1946
Original Poster

hello hope you all are well

i am new to development and i want to run and get result from geotab api on my-server

please someone help me and tell me how to do it .

what i am doing is authenticating and getting session-id and then run the provided script given in geotab developer but authentication is working and script is giving me error

and i dont have any idea what to do some one please help.

 

Join the conversation

You need to be logged in to reply to this post and participate in the Geotab Community discussions.

10 Replies

Hey @MBost-1946​,

 

Can you post your code so I can help you figure out what the issue is please?

 

Thanks,

Tucker

MBost-1946
Original Poster

<?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

 

What error are you getting? Also I would recommend using the PHP Client that Geotab created if you haven't looked at it yet. It should simplify a lot of what you're doing. Also if you decide not to use it (we didn't in our php project), I would recommend using something like Guzzle to make your requests.

MBost-1946
Original Poster

there is no error now but now i am getting the reponse no device-info found

and thankyou for response

 

Can you send a screenshot of the output of your script please?

MBost-1946
Original Poster

222this is the result

 

I don't see anything wrong with the script at first glance, have you tried debugging it? What's in $statusInfos after the request?

MBost-1946
Original Poster

Thankyou for your response

Authentication Response:Array

(

[result] => Array

(

[credentials] => Array

(

[database] => ********

[sessionId] => MyZpooV-eDib8WT5q7lIUA

[userName] => *************

)

 

[path] => ThisServer

)

 

[jsonrpc] => 2.0

)

DeviceStatusInfo Response:Array

(

[error] => Array

(

[message] => The Credentials object is missing or empty.

[code] => -32000

[data] => Array

(

[id] => 888ec4c3-1888-45fb-817f-541bf16feba7

[type] => ArgumentException

[requestIndex] => 0

)

 

[name] => JSONRPCError

[errors] => Array

(

[0] => Array

(

[message] => The Credentials object is missing or empty.

[name] => ArgumentException

)

 

)

 

)

 

[jsonrpc] => 2.0

[requestIndex] => 0

)

after debuggung i get this

Oh one thing I didn't notice before is that if you have a session_id, you're not sending the username with it. so your credentials array isn't correct (and I think it might need to be an object based on the response, but not 100% sure on that). The session is tied to a user, so you need to include both. I would really recommend switching to use the php client that I linked in the other post because it will handle things like this for you and really simplify this for you. If you want to do anything more complex than this simple script, it will end up saving you a ton of time. Let me know if fixing the credentials also fixes this issue. If it doesn't, can you send the responses again?

 

Thanks,

Tucker

MBost-1946
Original Poster

ok i will try thankyou😇

Still have questions?