Q:

How to Send Text Messages With PHP?

belongs to collection: PHP Programming Exercises

0

 PHP code to send SMS to mobile from website

How to Send Text Messages With PHP

In this exercise, you will learn how to send SMS to a mobile phone from a website using PHP programming language.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Text messaging has become incredibly inescapable all through the world. It reduces costs, provides efficient service delivery, enhances marketing promotions, and improves staff communications. 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 fulfill this requirement, we used to have an HTML form that allows 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 '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>

PHP Send SMS Form

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 three entities you basically 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Programming Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to convert stdClass object to Array in PHP?... >>
<< How to send email with SMTP in PHP?...