How to send sms using PHP program with examples

Sending SMS using PHP can be done by using an SMS gateway service. An SMS gateway service is a service that allows you to send SMS messages via an API (Application Programming Interface) using various programming languages including PHP. There are several SMS gateway services available such as Twilio, Nexmo, and Plivo, which provide APIs to send SMS messages.

PHP code to send SMS using Twilio API

Here is an example of how to send an SMS using the Twilio API in PHP:

<?php
require_once 'path/to/twilio-php/Services/Twilio.php';
$account_sid = 'your_account_sid';
$auth_token = 'your_auth_token';
$client = new Services_Twilio($account_sid, $auth_token);
$message = $client->account->sms_messages->create(
    '+1234567890', // From a valid Twilio number
    '+0987654321', // Text this number
    "Hello, this is a test SMS sent using the Twilio API in PHP."
);
echo "SMS sent successfully";
?>

You will need to sign up for an account with Twilio, and obtain account sid and auth token to use in the code above. Also, you will need to purchase a phone number from Twilio to send SMS. You can then replace the +1234567890 with your Twilio phone number, and +0987654321 with the recipient’s phone number.

Other SMS gateways have similar way of sending SMS via API, you will need to check their documentation for more information on how to use their API to send SMS from PHP.

It’s important to note that sending SMS via an SMS gateway service typically requires a paid subscription and may incur additional costs for sending SMS messages.

PHP code to send SMS using Nexmo API

Here is an example of how to send an SMS using the Nexmo API in PHP:

<?php
$url = 'https://rest.nexmo.com/sms/json?' . http_build_query([
    'api_key' => 'your_api_key',
    'api_secret' => 'your_api_secret',
    'to' => 'recipient_phone_number',
    'from' => 'your_nexmo_number',
    'text' => 'Hello, this is a test SMS sent using the Nexmo API in PHP.'
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if($response->messages[0]->status != 0) {
    echo "SMS sent successfully";
} else {
    echo "SMS sending failed";
}
?>

You will need to sign up for an account with Nexmo, and obtain API key and secret to use in the code above. Also, you will need to purchase a phone number from Nexmo to send SMS. You can then replace the recipient_phone_number with the recipient’s phone number, and your_nexmo_number with your Nexmo phone number.

As you can see, this code uses the curl library to make a POST request to the Nexmo API endpoint with the necessary parameters to send an SMS. It’s important to check the status of the message after sending it, by looking at the ‘status’ of the returned message.

It’s important to note that sending SMS via an SMS gateway service typically requires a paid subscription and may incur additional costs for sending SMS messages.

Leave a Comment

Related Posts