Update users using API Error

Enrique Luis 6 years ago

Hello. I had problems using the API to update data from PHP.
This is the code that is posted in the forum, but dont let me make changes:

$email='test1';
$password='123';
$id='2';
$name='pocho';

$t=traccar::login($email,$password);

if($t->responseCode=='200') {

   $traccarCookie = traccar::$cookie;
  
   $t = traccar::updateUser($id,$name,$email,$password,$cookie);     // line 188

   if($t->responseCode=='200') {

       echo 'ok';
   }
   else {

       $response=json_decode($t->response);

       echo $response->details;          // line 188
   }
}
else echo 'Incorrect email address or password';

ERROR
Notice: Undefined variable: cookie in C:\xampp\htdocs\funcapi.php on line 188
Notice: Trying to get property of non-object in C:\xampp\htdocs\funcapi.php on line 198

The login is ok
Thank you for the help.
Regards
Enrique

jaimzj6 years ago

Enrique, you have only shared the part where you are calling the function, but the errors clearly show that in file 'funcapi.php' there are some issues, how about sharing 'funcapi.php'

Enrique Luis 6 years ago

Hello jaimzj. I apologize for my English.
Thank you for the response!
This code i found post it in stackoverflow by Anton Tananaev, i believe.
I changed '$cookies for '$traccarCookie' in line 188 and work well with other functions like "addusers", but still giveme the error in line 198
(Notice: Trying to get property of non-object in C:\xampp\htdocs\funcapi.php on line 198), when try the function "updateUser" and not maked any changes in the database.
I've been struggling with this for a week now and I can not make it work.
Not let me put all the code here because is too long.
This is the link from where i take the code:
https://github.com/branislavkeselj/traccar-api-php/blob/master/traccar.php

I make this changes:

$email='test1';
$password='123';
$id='2';
$name='pocho';

$t=traccar::login($email,$password);

if($t->responseCode=='200') {

    $traccarCookie = traccar::$cookie;

    $t = traccar::updateUser($id,$name,$email,$password,$traccarCookie);   //Here i change $cookie to $traccarCookie - LIne 188 (now working) 

    if($t->responseCode=='200') {

        echo 'ok';
    }
    else {

        $response=json_decode($t->response);

        echo $response->details;          // line 198
    }
}
else echo 'Incorrect email address or password';

Thank you very much. Regards.

jaimzj6 years ago

Enrique,

You will need to share with me at-least that part of the code from the funcapi.php file which holds the function updateUser()
so that I can see what is happening, Because the code on the github repository you shared might be outdated.

Also what version of traccar are you using?

Enrique Luis 6 years ago

jaimzj,
Its possible that the function need all the fields to work?
This is the code that i use:

public static function updateUser($id,$name,$email,$password,$cookie) {
        $data='{"id":'.$id.',"name":"'.$name.'","email":"'.$email.'","readonly":false,"admin":false,"map":"","distanceUnit":"","speedUnit":"","latitude":0,"longitude":0,"zoom":0,"twelveHourFormat":false,"password":"'.$password.'"}';

        return self::curl('/api/users/'.$id, 'PUT',$cookie ,$data,array('Content-Type: application/json'));
    }

And the call is:

$email='test1';
$password='123';
$id='2';
$name='pocho';

$t=traccar::login($email,$password);

if($t->responseCode=='200') {

    $traccarCookie = traccar::$cookie;

    $t = traccar::updateUser($id,$name,$email,$password,$traccarCookie);   //Here i change $cookie to $traccarCookie - LIne 188 (now working) 

    if($t->responseCode=='200') {

        echo 'ok';
    }
    else {

        $response=json_decode($t->response);

        echo $response->details;          // line 198
    }
}
else echo 'Incorrect email address or password';
Enrique Luis 6 years ago

And the version is 3.16, i forgot that.
I am runing traccar in Windows 2012 R2.

jaimzj6 years ago

Enrique,

I can see that you have modified the functions' it is not the same as what it is on the link you shared with me that you took the code from.
I will share a working code with you shortly...

jaimzj6 years ago

Here is a working example for updateUser, please note i have hard coded most values, so you might want to edit the function to make it dynamic to your need.

public static function updateUser($cookie,$id,$name,$email,$password,$attributes) {

$id = $id;
$name = $name;
$email = $email;
$password = $password;
$attributes = '{}';
$admin = "false";
$coordinateFormat = "0";
$deviceLimit = "-1";
$deviceReadonly = "false";
$disabled = "false";
$expirationTime = "";
$latitude = "0";
$limitCommands = "false";
$login = "";
$longitude = "0";
$map = "";
$phone = "";
$poiLayer = "";
$readonly = "false";
$token = "";
$twelveHourFormat = "false";
$userLimit = "-1";
$zoom = "0";


    $data='{"id":"'.$id.'","name":"'.$name.'","email":"'.$email.'","admin":"'.$admin.'","coordinateFormat":"'.$coordinateFormat.'","deviceLimit":"'.$deviceLimit.'","deviceReadonly":"'.$deviceReadonly.'","disabled":"'.$disabled.'","expirationTime":"'.$expirationTime.'","latitude":"'.$latitude.'","limitCommands":"'.$limitCommands.'","login":"'.$login.'","longitude":"'.$longitude.'","map":"'.$map.'","phone":"'.$phone.'","poiLayer":"'.$poiLayer.'","readonly":"'.$readonly.'","token":"'.$token.'","twelveHourFormat":"'.$twelveHourFormat.'","userLimit":"'.$userLimit.'","zoom":"'.$zoom.'","password":"'.$password.'","attributes":'.$attributes.'}';


    return self::curl('/api/users/'.$id,'PUT',$cookie ,$data,array(self::$json));
}
Enrique Luis 6 years ago

jaimzj,
It worked!
In my case I had to change:

return self :: curl ('/ api / users /'.$ id,' PUT ', $ cookie, $ data, array (self :: $ json));

For this:

return self :: curl ('/ api / users /'.$ id,' PUT ', $ cookie, $ data, array (' Content-Type: application / json '));

In case someone is missing at some time.

Thank you very much for the help, I really appreciate all the time and effort.
Regards. Enrique