Forward events to PHP application

Evanderson3 years ago

Has anyone managed to send the events to the PHP application?

<entry key='forward.enable'>true</entry>
<entry key='forward.json'>true</entry>
<entry key='forward.url'>https://127.0.0.1/teste</entry>
<entry key='event.forward.enable'>true</entry>
<entry key='event.forward.url'>https://localhost/teste</entry>
<entry key='event.forward.header'>Content-Type: application/json; charset=utf-8</entry>

In Php:

var_dump($_POST);

I enabled notifications on the WEB, which I receive by email, but does not arrive in PHP.

Anton Tananaev3 years ago

Usually it means that there's a problem with your server. For example, https://127.0.0.1 is guaranteed to have an invalid SSL certificate. You can't have a valid certificate without a domain name.

Evanderson3 years ago

Hi Anton,

Yes, I have a valid SSL certificate.

Anton Tananaev3 years ago

An IP address cannot have a valid certificate by definition.

Evanderson3 years ago

Hi Anton,

<entry key='forward.url'>http://127.0.0.1/teste</entry>

It still doesn't work.

In the Traccar log it looks like this:

WARN: Position forwarding failed: 0 pending
Evanderson3 years ago

Hi Anton,

I managed to forward the positions like this:

<entry key='forward.url'>https://rastrear.quarkgps.com/teste?id={uniqueId}&amp;name={name}&amp;status={status}&amp;protocol={protocol}&amp;fixtime={fixTime}&amp;statuscode={statusCode}&amp;attributes={attributes}</entry>

But it just worked out because I took that line:

<entry key='forward.enable'>true</entry>

But my objective is the events, they are configured in this way but I don't receive anything.

<entry key='event.forward.enable'>true</entry>
<entry key='event.forward.url'>http://localhost/teste?event&amp;position&amp;users</entry>
Anton Tananaev3 years ago

I suspect your server only accepts GET requests. You need to listen to POST if you are using JSON.

Piotr3 years ago

You need to parse request body in your PHP script.
Something like this:
$data = json_decode(file_get_contents('php://input'));

davejh3 years ago

This is how I do it to get all notifications from traccar. It works for me. hope it helps.


$json = file_get_contents('php://input');

$json_data = json_decode($json);

$data = array(
    'to' => $json_data->to,
    'message' => $json_data->message,
);
Evanderson3 years ago

Hi Davejh

How is it in your config?

davejh3 years ago

Hi Evanderson This is a simplified way I get whatever the config posts to my test.php file which is the sent to the traccar sms gateway app.
But of course you use this to do what ever you want with the data received using

file_get_contents('php://input');

The traccar config simply posts the recipients mobile number and message like this:

<entry key='notificator.types'>web,mail,sms</entry>
<entry key='notificator.sms.manager.class'>org.traccar.sms.HttpSmsClient</entry>
<entry key='sms.http.url'>https://yourdomain.com/test.php</entry>
<entry key='sms.http.template'>
    {
        "to": "{phone}",
        "message": "{message}"
    }
</entry>



Then in test.php

<?php
$json = file_get_contents('php://input');

$ip = 'http://11.222.333.44/';
$apikey = '8f700651';



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ip);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{$json}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: ' . $apikey)
);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
davejh3 years ago

In the example above I am sending the whole post directly to test php which in turn sends it to the traccar sms gateway to fire off the sms.

But if you want to break it down use:

$json = file_get_contents('php://input');

$json_data = json_decode($json);

$to=$json_data->to;
$message=$json_data->message;
davejh3 years ago

I have had to learn php quickly for a client. so if i have done anything wrong here please let me know. PHP is not really my thing so my knowlege is limited. My client wanted a way of sending the sms without sending the api key or the real url of the sms gateway in a post from the traccar config.

Evanderson3 years ago

Hi Davajh,

Thank you, I will test.

Neo75303 years ago

https://github.com/neo7530/traccar_pusher take a look at this. I use this for pushing alarms and notifications via pushover and gotify.