How to send SMS using PHP program (full code sample included) - Biz Tech

How to send SMS using PHP program (full code sample included)

Listen

First, you need to sign up for a Twilio account and get your Account SID and Auth Token. You’ll also need to get a Twilio phone number that you can use to send SMS messages.

Once you have all of that information, you can use the following code:

<?php
// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'your_account_sid';
$auth_token = 'your_auth_token';
// Your Twilio phone number
$twilio_number = "your_twilio_phone_number";

// The recipient's phone number, in the format +14155552671
$to_number = "+14155552671";

// The SMS message you want to send
$message = "Hello, World!";

// Initialize the Twilio client
$client = new Twilio\Rest\Client($account_sid, $auth_token);

// Send the SMS message
$message = $client->messages->create(
    $to_number, // recipient's phone number
    array(
        'from' => $twilio_number, // your Twilio phone number
        'body' => $message
    )
);

// Print the message SID
echo "Message SID: " . $message->sid;
?>

 

Make sure to replace the your_account_sid, your_auth_token, your_twilio_phone_number, +14155552671 and Hello, World! with your own Twilio account details and the SMS message you want to send.

This code uses the Twilio PHP library to send an SMS message. You’ll need to make sure that the Twilio PHP library is installed on your server before you can use this code. You can install the library using Composer or by downloading the source code from the Twilio PHP GitHub repository.