Traccar SMS Gateway for None traccar use

davejh 5 years ago

Firstly Id like to say thanks to Anton for such a great app. Its works flawlessly with my traccar setup. The reason for my post is that I want to use the app from my website to send sms messages using php. I'm sure there must be a simple way to do this with a for. However ive tried countless times with no results. I wonder if somebody could come up with something. I see in the traccar config it simply sends the phone number and message along with the api key. Any ideas anyone?

Anton Tananaev 5 years ago

What is the issue exactly? Do you get some errors? What are troubleshooting results so far?

davejh 5 years ago

I am trying to post my form to this php script. but no results. the form sends the recipient phone number, message and the api key to this script

<?php
$phone = $_POST['to'];
$text = $_POST['message'];
$api_key = $_POST['api_key'];
$enviar = $_POST['enviar'];

  if (isset($enviar)) {
$url = 'http://myip';

$data = array(
    'to' => $phone,
    'message' => $text,
);
$curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            "Authorization: " . $api_key 
                                                    )
                    );  }

$response = curl_exec($curl);
$obj = json_decode($response, true);
var_dump($response);
davejh 5 years ago

traccar works perfect

davejh 5 years ago
string(10) "Error: 500"
Anton Tananaev 5 years ago

Compare your request with Traccar request.

davejh 5 years ago

Hi. Could you show me the traccar code or form that makes the request please

davejh 5 years ago

Is there anyone else could help with this. I would be happy to pay.

Anton Tananaev 5 years ago

If you are interested in paid support, you can send an email to support address to discuss it.

davejh 5 years ago

Yes i'll do that now thanks

davejh 5 years ago

in case anyone is interested its very easy to send a message to the traccar sms gateway app using the command line as follows.

curl -H "Content-Type: application/json" -H "Authorization: your_api_key" -d '{"to":"+44123456789","message":"test"}' http://00.000.000.00/

Does anyone want to contribute to this post with other methods of sending messages to the app.

davejh 5 years ago

Also in php like this:

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://00.000.000.00/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"to\":\"+44123456789\",\"message\":\"test message\"}");

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: your_api_key';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

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

In the php example above you need to put a \ before a " for some reason this forum strips them out

Anton Tananaev 5 years ago

You just need to use proper markdown.

davejh 5 years ago

And this php code is a bit more tidy

<?php

$apikey = 'Your API Key';
$to = '+44123456789';
$message = 'this is a test';
$mobile_ip = 'http://00.000.000.00/';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $mobile_ip);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"to\":\"$to\",\"message\":\"$message\"}");

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);