How Do You Send SMS Using PHP Script?
I
f you are developing eCommerce project than definitely you need to add feature send sms notification for site users. Sending message to user can also be used to send coupon code, let user know your bill or statement is ready etc. Sending SMS from php script is simple and tricky. This tutorial shows quick and easy way to send sms using php script. Hope its cool and help you in development. We will use twilio sms library to send SMS. Twilio is best library for sending or receiving call as well as message.
Very first step to create twilio account. Twilio provides limited number of sms in trial account. Remember if you are using twilio trial account than you need to verity number first on twilio on which you want to receive text message. Otherwise you will not get text message.
index.php
lib
-- Twilio
-- Twilio.php
Contains basic form which inputs phone number and message from user.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<body>
<form action="" name="frmMessage" method="post">
<div>Send SMS using PHP script</div>
<div>
<label>Phone Number:</label>
<input type="text" name="phoneNumber" value="" />
</div>
<div>
<label>Message:</label>
<textarea name="message"></textarea>
</div>
<div>
<input type="submit" value="Send" />
</div>
</form>
</body>
|
Include twilio library to access send sms service. Download twilio php library from here. Replace your twilio Account id, Authentication token and twilio number in below code. We have added validations for phone number and message than initialised twilio library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
require "lib/Twilio/Twilio.php";
$errors = array();
if(!empty($_POST[['phoneNumber']]) || !empty($_POST['message'])){
if(empty($_POST['phoneNumber'])){
$errors[] = "Please enter phone number";
}else if(!empty($_POST['phoneNumber']) && !is_numeric($_POST['phoneNumber'])){
$errors[] = "Please enter valid phone number";
}else if(empty($_POST['message'])){
$errors[] = "Please enter message";
}else{
$phoneNumber = $_POST['phoneNumber'];
$message = $_POST['message'];
$AccountSid = "{{ account_sid }}"; // Your Account SID from www.twilio.com/console
$AuthToken = "{{ auth_token }}"; // Your Auth Token from www.twilio.com/console
$client = new Services_Twilio($AccountSid, $AuthToken);
$message = $client->account->messages->create(array(
"From" => "+12345678901", // From a valid Twilio number [Get it from your account]
"To" => $phoneNumber, // Text this number
"Body" => $message
));
// Confirmation message
echo "Sent message {$message->sid}";
}
}
|
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<?php
require "/Twilio/Twilio.php";
$errors = array();
if(!empty($_POST[['phoneNumber']]) || !empty($_POST['message'])){
if(empty($_POST['phoneNumber'])){
$errors[] = "Please enter phone number";
}else if(!empty($_POST['phoneNumber']) && !is_numeric($_POST['phoneNumber'])){
$errors[] = "Please enter valid phone number";
}else if(empty($_POST['message'])){
$errors[] = "Please enter message";
}else{
$phoneNumber = $_POST['phoneNumber'];
$message = $_POST['message'];
$AccountSid = "{{ account_sid }}"; // Your Account SID from www.twilio.com/console
$AuthToken = "{{ auth_token }}"; // Your Auth Token from www.twilio.com/console
$client = new Services_Twilio($AccountSid, $AuthToken);
$message = $client->account->messages->create(array(
"From" => "+12345678901", // From a valid Twilio number
"To" => $phoneNumber, // Text this number
"Body" => $message
));
// Display a confirmation message on the screen
echo "Sent message {$message->sid}";
}
}
?>
<html>
<body>
<?php
if (!empty($errors)) {
foreach ($errors as $error) {
echo '<p class="cls-error">'.$error.'</p>';
}
}
?>
<form action="" name="frmMessage" method="post">
<div>Send SMS using PHP script</div>
<div>
<label>Phone Number:</label>
<input type="text" name="phoneNumber" value="" />
</div>
<div>
<label>Message:</label>
<textarea name="message"></textarea>
</div>
<div>
<input type="submit" value="Send" />
</div>
</form>
</body>
</html>
|
That’s It. We are ready to send SMS using PHP script now. Comment or email me on [email protected] for further help in development.
kristian
hi thanks for your help
i am from peru
i have a problem , the library TWILIO.PHP
I do not find to download
please could you help me
Thank you !!
AnnaMJ
Hello! My name is AnnaMarkova, our company need to advertise on your website. What is your prices? Thank you. Best regards, Mary.
Gopal Joshi
Hi AnnaMarkova,
For Advertisement please email me details of advertise on
Thank You
MaxGabrowef
Profit work! Thanx you respecting you problem
http://sanekdoors.cz/node/79
tej
How to get the Lib/TWILIO folder?
Doraviami
Hi Gopal!