PHP code to send SMS to mobile from website
In this article, you will learn how to send SMS to a mobile phone from a website using the PHP programming language.
Text messaging has become incredibly inescapable all over the world. It reduces costs, provides efficient service delivery, enhances marketing promotions, and improves staff communication. It can connect faster with short messages containing crucial bits of information, rather than long conversations. Since mobile phones are almost always within our arm's reach, SMS is landing straight into the customer's hands. SMS is very personal because it's delivered directly to your customer's mobile phone.
This application allows us to compose a message. In order to send an SMS from a website to a mobile, we need a third party API. The SMS gateway allows a computer system to send or receive SMS to or from a telecommunications network. There are lots of free or commercial SMS gateways available.
To fulfil this requirement, we used to have an HTML form that allowed the user to enter the required SMS details, i.e., phone number and message text. On submitting the form, the data is posted to a PHP script, where we have sent the SMS text through a gateway.
index.php
Suppose we have the following interface to get the phone number of the recipient and the message text from the website visitors. When the user clicks on the 'Send Message' button, the data will be posted to the 'phpsendsms.php' page.
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<h1>PHP Send SMS</h1>
<form method="post" action='phpsendsms.php'>
<div class="form-group">
<label for="phoneno">Mobile Number</label>
<input type="text" name="phoneno" class="form-control" placeholder="Enter Phone Number" >
</div>
<div class="form-group">
<label for="exampleFormControlTextarea3">Enter Text Message</label>
<textarea class="form-control" name="smstext" rows="7"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary" value="Send Message">
</div>
</form>
</div>
</body>
</html>

phpsendsms.php
This file is responsible for sending SMS. It collects the form data and configures the authentication key and sender id. The API gateway URL is used to pass the SMS to the gateway. So, there are basically three entities you need from your configuration gateway- Authentication Key, Sender ID and Gateway URL. The curl_setopt_array() method is used here to transfer multiple options to a CURL.
Make sure to replace the YOUR_AUTH_KEY, YOUR_SENDER_ID and YOUR_GATEWAY_URL.
<?php
// Authentication key
$authKey = "YOUR_AUTH_KEY";
// Also add muliple mobile numbers, separated by comma
$phoneNumber = $_POST['phoneno'];
// route4 sender id should be 6 characters long.
$senderId = "YOUR_SENDER_ID";
// Your message to send
$message = urlencode($_POST['smstext']);
// POST parameters
$fields = array(
"sender_id" => $senderId,
"message" => $message,
"language" => "english",
"route" => "p",
"numbers" => $phoneNumber,
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "YOUR_GATEWAY_URL",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($fields),
CURLOPT_HTTPHEADER => array(
"authorization: ".$authKey,
"accept: */*",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
The above code returns TRUE with message id if all options were successfully set. If an option could not be successfully set, FALSE is immediately returned.
Related Articles
PHP reverse a string without predefined functionPHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
PHP String Contains
PHP remove last character from string
Electricity bill program in PHP
PHP import Excel data to MySQL using PHPExcel
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP