Are you planning to provide any context, like what version you're using and if you're even using an official release?
Having same error. Server ver 5.8
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class gps {
public static $host = 'https://server.server.net';
private static $adminEmail = 'admin@gmail.com';
private static $adminPassword = 'adminpass';
public static $cookie;
private static $jsonA = 'Accept: application/json';
private static $jsonC = 'Content-Type: application/json';
private static $urlEncoded = 'Content-Type: application/x-www-form-urlencoded';
//Server
public static function server()
{
return self::curl('/api/server?' . $data, 'GET', $sessionId, '', array());
}
//Session
public static function loginAdmin()
{
return self::login(self::$adminEmail, self::$adminPassword);
}
public static function userAdd($sessionId, $name, $email, $password, $attributes)
{
$id = '-1';
$name = $name;
$email = $email;
$password = $password;
$readOnly = 'false';
$administrator = 'false';
$map = '';
$latitude = '0';
$longitude = '0';
$zoom = '0';
$twelveHourFormat = 'false';
$coordinateFormat = '0';
$disabled = 'false';
$expirationTime = '';
$deviceLimit = '-1';
$userLimit = '-1';
$deviceReadonly = 'false';
$limitCommands = 'false';
$attributes = $attributes;
$data = '{"id":"' . $id . '","name":"' . $name . '","email":"' . $email . '","readonly":"' . $readOnly . '","administrator":"' . $administrator . '","map":"' . $map . '","latitude":"' . $latitude . '","longitude":"' . $longitude . '","zoom":"' . $zoom . '","password":"' . $password . '","twelveHourFormat":"' . $twelveHourFormat . '","coordinateFormat":"' . $coordinateFormat . '","disabled":"' . $disabled . '","expirationTime":"' . $expirationTime . '","deviceLimit":"' . $deviceLimit . '","userLimit":"' . $userLimit . '","deviceReadonly":"' . $deviceReadonly . '","limitCommands":"' . $limitCommands . '","attributes":' . $attributes . '}';
$result = self::curl('/api/users', 'POST', $sessionId, $data, array(self::$jsonC));
// Проверка на null перед вызовом метода getAdministrator()
if ($result->responseCode == 200 && isset($result->response) && !is_null($result->response)) {
$response = json_decode($result->response);
if (isset($response->administrator)) {
$administrator = $response->administrator;
}
}
return $result;
}
public static function login($email,$password){
$data='email='.$email.'&password='.$password;
return self::curl('/api/session','POST','',$data,array(self::$urlEncoded));
}
public static function curl($task,$method,$cookie,$data,$header) {
$res=new stdClass();
$res->responseCode='';
$res->error='';
$header[]="Cookie: ".$cookie;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::$host.$task);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($method=='POST' || $method=='PUT' || $method=='DELETE') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
$data=curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
if (preg_match('/^Set-Cookie:\s*([^;]*)/mi', substr($data, 0, $size), $c) == 1) self::$cookie = $c[1];
$res->response = substr($data, $size);
if(!curl_errno($ch)) {
$res->responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
}
else {
$res->responseCode=400;
$res->error= curl_error($ch);
}
curl_close($ch);
return $res;
}
}
// Пример использования для добавления пользователя
$name = 'John Doe';
$email = 'johndoe@example.com';
$password = 'secret';
$attributes = '{"age": 30, "city": "New York"}';
$sessionId = gps::loginAdmin()->response;
//var_dump($sessionId);
echo"<br>";
$result = gps::userAdd($sessionId, $name, $email, $password, $attributes);
var_dump($result);
if ($result->responseCode == 200) {
echo 'Пользователь успешно добавлен';
} else {
echo 'Ошибка при добавлении пользователя: ' . $result->error;
}
?>
Does it work if you do it from the web app?
Yes, if i add from usual interface it works.
Then you're clearly doing something wrong. The official web app uses exactly the same API. Compare the requests and you should have an answer.
I used this code as a sample:
https://github.com/zeustd/traccar-api-php/blob/master/api.php
Maybe api version 4.3 is changed much after 5.8?
Thank you
I don't think it changed in any significant way.
Can you help please to figure out? I do not understand what am i doing wrong...
object(stdClass)#1 (3) { ["responseCode"]=> int(400) ["error"]=> string(0) "" ["response"]=> string(258) "Cannot invoke "org.traccar.model.User.getAdministrator()" because the return value of "org.traccar.api.security.PermissionsService.getUser(long)" is null - NullPointerException (PermissionsService:78 < *:182 < UserResource:76 < ... < OverrideFilter:50 < ...)" }
I already gave you a suggestion. Have you done it?
I had to use a mysql table to work with server. This looks more easy then work with an API of the server...
Updating database directly doesn't refresh the internal cache, so you might have some problems. But it works better for you, then it's all good.
Which problems can be?
If you send anything other than name, email and password on the payload, this error pops. Happens with the latest codebase 50e730c0cbbe253b68a2db0e57920ddc3a1b3814
Well that makes sense, since this is a public endpoint, if someone send a payload with admin:true
, for example, you would be in trouble lol