0

I have an SMS API that I need to use to send a text message on a site that I'm building. There is a form on the website that collects a few details and the phone number, the message has to go to the phone number specified in the form.

The API link I have is:

http://URL/api/v3/?method=sms&api_key=XXd9e5XXXXXXXXXXXXX&to=197XXXXXXX&sender=INFXXX&message=Welcome%20to%20messaging

How can I do this POST request using Ajax?

var formData = {
    'name': name,
    'location': location,
    'mobile': mobile
};
$.ajax({
    type: 'POST',
    url: 'upload.php',
    data: formData,
    dataType: 'json',
    encode: true
})

This is what I use with the form on the site, can I use the same with this API?

2
  • depends on your api, read the documentation Commented Sep 19, 2016 at 5:35
  • As stated by @madalinivascu it depends on the your API. If you can provide the SMS gateway name may be someone will guide you in right direction. Commented Sep 19, 2016 at 6:27

1 Answer 1

2

In your upload.php file make a curl request like this:

upload.php:

$encodedMsg = urlencode($message);

$url = // url api url with mobile number and url emcoded message in it

// parameter to make a curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$APIresponse = curl_exec($ch);
curl_close($ch);
$res = json_decode($APIresponse, true);

if($res['ErrorCode'] == '000')      // check for the response return by the API
{
    $response   = 'Success';
}
else
{
    $response   = 'Failed';
}

echo $response;

// Use this response in your ajax success function
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.