POST API session

Osama Ahmed4 years ago
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://135.125.140.197:8082/api/session',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => 'email=j.vana%40ris******.com&password=R!%40****%252***',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/x-www-form-urlencoded',
    'Cookie: JSESSIONID=node0spbxks097p05bxx5mupjqry13638.node0'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

for this code i am getting response

{
    "id": 1,
    "attributes": {},
    "name": "Ri******",
    "login": "",
    "email": "j.vana@ri******.com",
    "phone": "",
    "readonly": false,
    "administrator": true,
    "map": "carto",
    "latitude": 0.0,
    "longitude": 0.0,
    "zoom": 2,
    "twelveHourFormat": true,
    "coordinateFormat": "dms",
    "disabled": false,
    "expirationTime": null,
    "deviceLimit": -1,
    "userLimit": 0,
    "deviceReadonly": false,
    "token": null,
    "limitCommands": false,
    "poiLayer": "",
    "password": null
}

How can i get token for further apis and etc.

Anton Tananaev4 years ago

You should get a cookie session in the header.

Osama Ahmed4 years ago

I am sending Cookie in the header.

Osama Ahmed4 years ago

Response Header:

Date: Thu, 18 Mar 2021 07:03:59 GMT
Content-Type: application/json
access-control-allow-headers: origin, content-type, accept, authorization
access-control-allow-credentials: true
access-control-allow-methods: GET, POST, PUT, DELETE, OPTIONS
access-control-allow-origin: *
Content-Length: 401
Server: Jetty(9.4.36.v20210114)

Response Body

Osama Ahmed4 years ago

Can anyone help me to integrate this traccar. I am not able to get token and without token cannot proceed. please

Anton Tananaev4 years ago

What token and why do you need it?

Osama Ahmed4 years ago
        $adminLoginResponse = gps::loginAdmin();
        $sessionId = gps::$cookie;
        $_SESSION['sessionId'] = $sessionId;
        //dd($sessionId);
        $tokenResponse = gps::session($sessionId);
        $response = $tokenResponse->response;
        $token = json_decode($response);
        $token_traccar = $token->token;

        $test = gps::login(env('USR_TRACCAR'),env('PWD_TRACCAR'));
        $token = json_decode($test->response);
        $tc_token = $token->token;
Osama Ahmed4 years ago

in login function curl is being called and the curl i have posted above and the response also.

return self::curl('/api/session','POST','JSESSIONID=node0spbxks097p05bxx5mupjqry13638.node0',$data,array(self::$urlEncoded));
Anton Tananaev4 years ago

Great conversation.

Osama Ahmed4 years ago

What am i doing wrong here sir.

Anton Tananaev4 years ago

Probably everything if you cannot even explain what you are trying to achieve.

Osama Ahmed4 years ago

I am trying to login first thing and get session id so i can communicate with the api easily.

Osama Ahmed4 years ago

later i will trying to get gps locations of the devices.

Anton Tananaev4 years ago

And how do you plan to use that token exactly?

Osama Ahmed4 years ago
        $adminLoginResponse = gps::loginAdmin();
        $sessionId = gps::$cookie;
        $_SESSION['sessionId'] = $sessionId;
        //dd($sessionId);
        $tokenResponse = gps::session($sessionId);
        $response = $tokenResponse->response;
        $token = json_decode($response);
        $token_traccar = $token->token;

        $test = gps::login(env('USR_TRACCAR'),env('PWD_TRACCAR'));
        $token = json_decode($test->response);
        $tc_token = $token->token;

        $groupsResponse = gps::groups($sessionId);
        $response3 = $groupsResponse->response;
        $groupes = json_decode($response3);

        $positionsResponse = gps::positions($sessionId);
        $response4 = $positionsResponse->response;
        $positions = json_decode($response4);

        $devicesResponse = gps::devices($sessionId, '');
        $response2 = $devicesResponse->response;
        $devices = json_decode($response2);

        $geofencesResponse = gps::geofences($sessionId);
        $response5 = $geofencesResponse->response;
        $geofences = json_decode($response5);