[Solved] ADD user example using PHP and Traccar API  

  RSS

JBFelizberto
(@jbfelizberto)
Trusted Member
Joined: 2 months ago
Posts: 27
22/03/2019 2:15 pm  

Hi Jaimzi, First of all, congratulation for your initiative to use PHP <=>Traccar Api.

I am searching (and others people too) a for long time how add user in traccar-Api by PHP.

Do you have a pratical example for add user?

I am not a advanced developer user, so any helps will be appreciate.

JB


Quote
jaimzj
(@jaimzj)
Guru Admin
Joined: 11 months ago
Posts: 46
22/03/2019 2:20 pm  

Hello JB,

The feature is already implemented on this following link

https://github.com/gpsguru/traccar-api-php/blob/a86745c2e756fad07c1c85bf956d436ffcde25a1/traccarApi.php#L66

Have you tried it? For a practical example yes I can do it, however for that you will need to start and let me know at what point you are finding it difficult to complete.

also have a look at examples here. https://github.com/gpsguru/traccar-ui-php

 

 

Markers & maps are magical !!!


ReplyQuote
JBFelizberto
(@jbfelizberto)
Trusted Member
Joined: 2 months ago
Posts: 27
22/03/2019 2:40 pm  

I did know how use it (my fault of knowledge):

 

I create a file with: 

<?php
header('Content-Type: application/json');

include('traccarApi.php');

userAdd("","teste","teste@email.com","123456","")
?>

 

and I have the error:

PHP Fatal error:  Uncaught Error: Call to undefined function userAdd() in C:\ZAZrelatorios\api-use-example.php:6
Stack trace:
#0 {main}
  thrown in C:\ZAZrelatorios\api-use-example.php on line 6

 

This post was modified 2 months ago by jaimzj

ReplyQuote
jaimzj
(@jaimzj)
Guru Admin
Joined: 11 months ago
Posts: 46
22/03/2019 2:58 pm  

The error returned clearly shows where the problem is.

However here is what you need to correct in your code.

gps::userAdd($sessionId,$name,$email,$password,$attributes);

Markers & maps are magical !!!


ReplyQuote
JBFelizberto
(@jbfelizberto)
Trusted Member
Joined: 2 months ago
Posts: 27
22/03/2019 3:11 pm  
<?php
header('Content-Type: application/json');

include('traccarApi.php');

$name = "teste";
$email = "testes@email.com";
$password = "123456";

$attributes = "";

gps::userAdd($sessionId,$name,$email,$password,$attributes);

?>

 

 

 

PHP Notice:  Undefined variable: sessionId in C:\ZAZrelatorios\api-use-example.php on line 12
PHP Fatal error:  Uncaught Error: Call to undefined function curl_init() in C:\ZAZrelatorios\traccarApi.php:371
Stack trace:
#0 C:\ZAZrelatorios\traccarApi.php(91): gps::curl('/api/users', 'POST', NULL, '{"id":"-1","nam...', Array)
#1 C:\ZAZrelatorios\api-use-example.php(12): gps::userAdd(NULL, 'teste', 'testes@email.co...', '123456', '')
#2 {main}
  thrown in C:\ZAZrelatorios\traccarApi.php on line 371

 

This post was modified 2 months ago by jaimzj

ReplyQuote
jaimzj
(@jaimzj)
Guru Admin
Joined: 11 months ago
Posts: 46
22/03/2019 3:17 pm  

The errors are self explanatory. If you see the error the following are its interpretation 

1. You do not have CURL support in your PHP (so enable CURL on your PHP)

2. sessionId is not defined (So, first you need to have this sessionId value) have you logged in?

Markers & maps are magical !!!


ReplyQuote
JBFelizberto
(@jbfelizberto)
Trusted Member
Joined: 2 months ago
Posts: 27
22/03/2019 3:43 pm  

1. solved - thanks

2. Could you clarify pls. In my homologation environment

 - I use user/password admin:admin.

- I am logged in traccar web in the same time ( in another Chrome 's tab)

 

class gps {

public static $host='http://127.0.0.1:8082';
private static $adminEmail='admin';
private static $adminPassword='admin';

...

}

 

PHP Notice:  Undefined variable: sessionId in C:\ZAZrelatorios\api-use-example.php on line 12

This post was modified 2 months ago by jaimzj

ReplyQuote
JBFelizberto
(@jbfelizberto)
Trusted Member
Joined: 2 months ago
Posts: 27
22/03/2019 4:10 pm  

I am trying too:

<?php
header('Content-Type: application/json');

session_start();

include('traccarApi.php');

$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$a = gps::login($username, $password);
$sessionId = gps::$cookie;
$_SESSION['sessionId'] = $sessionId;
$response = $a->response;
$responseCode = $a->responseCode;
if($responseCode == '200'){

echo ('true');
}else{

echo 'Incorrect username/password';
}

//-------------------------------------------------------------------
//add user
$name = "teste";
$email = "testes@email.com";
$password = "123456";

$attributes = "";

gps::userAdd($sessionId,$name,$email,$password,$attributes);
//-------------------------------------------------------------------

?>

 

but  still I can not forward...

This post was modified 2 months ago by jaimzj

ReplyQuote
jaimzj
(@jaimzj)
Guru Admin
Joined: 11 months ago
Posts: 46
22/03/2019 5:36 pm  

What is the error that you see? I do not understand your last question. (you mentioned you still can not forward).

Markers & maps are magical !!!


ReplyQuote
JBFelizberto
(@jbfelizberto)
Trusted Member
Joined: 2 months ago
Posts: 27
22/03/2019 5:55 pm  

Sorry: google bad translation -- "but I could not make it work."

 

I have the error:

PHP Notice:  Undefined index: username in C:\ZAZrelatorios\api-use-example.php on line 9
PHP Notice:  Undefined index: password in C:\ZAZrelatorios\api-use-example.php on line 10


This post was modified 2 months ago 2 times by JBFelizberto
This post was modified 2 months ago 2 times by jaimzj

ReplyQuote
jaimzj
(@jaimzj)
Guru Admin
Joined: 11 months ago
Posts: 46
22/03/2019 6:15 pm  

The error you shared is irrelevant to the code. So follow these steps and let me know.

Try to do some basic troubleshooting.

1. Check if your login function works check the value it returns in $a->response; and $a->responseCode;

2. Check if your sessionId value exist (echo it and check its value).

3. Finally if login and sessionId is working as expected, Check your userAdd functions response; (echo it)

And share its response with me.

This post was modified 2 months ago 3 times by jaimzj

Markers & maps are magical !!!


ReplyQuote
JBFelizberto
(@jbfelizberto)
Trusted Member
Joined: 2 months ago
Posts: 27
23/03/2019 1:46 am  

1 - ok

2 - ok

3- fail (how I echo it?)

This post was modified 2 months ago by JBFelizberto
This post was modified 2 months ago by jaimzj

ReplyQuote
JBFelizberto
(@jbfelizberto)
Trusted Member
Joined: 2 months ago
Posts: 27
23/03/2019 2:30 am  

 

RESPONSE_2: Unexpected character ('}' (code 125)): expected a value at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@64781f0d; line: 1, column: 367]

RESPONSE_CODE_2: 400
This post was modified 2 months ago by jaimzj

ReplyQuote
jaimzj
(@jaimzj)
Guru Admin
Joined: 11 months ago
Posts: 46
23/03/2019 3:12 am  

Let me begin with a very important point, please dont post complete codes again and again only post portions of code that is necessary. And explain what you have done and what errors you saw etc in detail (This will help)

The issues almost all of that you have come across so far has to do with knowing what to do and how to do them in PHP.

Now coming back to the issue. if you look at your error (where you mentioned point 3 failed)

the error is self explanatory  this part of your code, Although you overlooked it completely. 

 

RESPONSE_2: Unexpected character ('}' (code 125)): expected a value at [Source: 


The way to fix it is, change your $attributes value to = "{}";

This post was modified 2 months ago by jaimzj

Markers & maps are magical !!!


ReplyQuote
JBFelizberto
(@jbfelizberto)
Trusted Member
Joined: 2 months ago
Posts: 27
23/03/2019 1:39 pm  

works!!!! thank you so much


ReplyQuote
Share:
  
Working

Please Login or Register