I am using gatewayapi.com to send sms from a php script. It works great. But when I try to call the same script from jQuery ajax it will not work. Can someone please tell me how to do it? This is my php code that works.
$recipients = ['myphonenumber'];
$url = "https://gatewayapi.com/rest/mtsms";
$api_token = "mytoken";
$json = [
'sender' => 'mysender',
'message' => 'mymessage',
'recipients' => [],
];
foreach ($recipients as $msisdn) {
$json['recipients'][] = ['msisdn' => $msisdn];
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch,CURLOPT_USERPWD, $api_token.":");
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($json));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
This is my javascript that do not work.
<script>
function send_sms() {
$.ajax({
dataType: "json",
type: "POST",
async: false,
url: 'sendsms.php',
data: {
message: 'mymessage'
},
cache: false,
success: function(data)
{
}
});
}
</script>
I also tried to call the php script from a cronjob but that do not work.