1

Trying to implement sending sms features in my ecommerce store.

I use service called esteria.lv and they provided me with API link that looks like this: http://api1.esteria.lv/send?api-key=api_key&sender=example.com&number=11223344&text=message

If the message is sent then it outputs message ID, now it outputs error number 3(unable to authenticate).

To get it working with my ecommerce store, I found this resource: http://www.ajax-cross-origin.com/examples/cross-origin.htm, and made this code:

$(function() {

$( '#btn' ).click(function(){
  $.ajax({
    crossOrigin: true,
    url: 'http://api1.esteria.lv/send?api-key=api_key&sender=example.com&number=11223344&text=message',
    success: function(data) {
      $( '#test' ).html(data);
    }
 });
 }); 
}); 

It works, but the problem is, it sends 6 messages (requests) instead of just one. I need just 1 request and one sent sms. Anyone have any suggestions?

maarcs
  • 115
  • 10
  • Are you going to put your secret api key in your javascript? So anyone can send messages using your api key just looking at your code... unless it's a free service, it's a really bad idea... I think you should rethink your logic, you should do an ajax request to your server with just number and message (no api key! this solves also the cross origin request) and then use the secret api key to send the text message from your server. – peppeocchi Nov 28 '15 at 10:00
  • I put the whole link there just to show how it looks, I will not put the API key there. Can you please explain how the code should look in this case: "you should do an ajax request to your server with just number and message (no api key! this solves also the cross origin request) and then use the secret api key to send the text message from your server." – maarcs Nov 28 '15 at 10:04

1 Answers1

0

To answer your comment, this is what you should do.

In your javascript you should have an ajax call to your server

// collect sms data
$.ajax({
  url: 'yourserver/handlesms',
  method: 'post',
  data: {
     sender: 'email@mail.com',
     number: '1234567',
     message: 'Test message'
  }
}).then(function (data) {
  alert("Message sent!");
});

In your server you should have an handler for sending the sms, something like (I don't know what's your platform, I'll just write a really simple php example)

$data = $_POST;
$apiKey = '12345643223213ds';

$endpoint = 'http://api1.esteria.lv/send';

// Create new curl request
$ch = curl_init($endpoint);
// curl settings, add your data, api key etc...
$result = curl_exec($ch);

// Result will contain the response from your api call
// Then you can send a result back to your client (js)
echo json_encode(['status' => 'Message sent!']);

This is just an example, the server code depends on your platform.

In this case you don't have any cross origin request (all the js request will be sent to your server, that then is in charge of contacting your sms provider and send the messages.

The problem that's executed 6 times I think depends on something else but it's hard to say without looking at the rest of the code (you can try debugging the click event on #btn and see how many times is executed every time you click on the button.

peppeocchi
  • 814
  • 1
  • 9
  • 22
  • I use Opencart, and I found why it sends 6 times, that is because the code was the same 6 times. (Dohh). I will try to implement this, looks much better than what I was thinking about. – maarcs Nov 28 '15 at 10:29
  • I cant seem to get a decent response there. I edited $endpont like this: $endpoint = 'http://api1.esteria.lv/send' . $apiKey + $data; and added curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); but all I can get is the whole page outputed as response – maarcs Nov 28 '15 at 11:27
  • I suggest you to have a look to a [curl example](http://codular.com/curl-with-php), I cannot say why the response is not what you're expecting but if your curl request is correct you should see the correct response. Check also for php errors, try to hardcode the url first to see if it's working or not, then try to dynamically build the url. – peppeocchi Nov 30 '15 at 12:57