To send SMS messages via Twilio using PHP, you will need to create a Twilio account and obtain your account SID, auth token, and a Twilio phone number. Here are the basic steps:
- Sign up for a Twilio account at https://www.twilio.com/try-twilio.
- After creating an account, obtain your account SID and auth token from the Twilio console. These will be used to authenticate API requests to Twilio.
- Purchase a Twilio phone number to use for sending SMS messages.
- Install the Twilio PHP helper library by running the following command in your project directory:
composer require twilio/sdk
PHP- Include the Twilio PHP helper library in your PHP script by requiring the autoload.php file:
require_once 'path/to/autoload.php';
PHP- Set up the Twilio client with your account SID and auth token:
use Twilio\Rest\Client;
$account_sid = 'your_account_sid';
$auth_token = 'your_auth_token';
$client = new Client($account_sid, $auth_token);
PHP- Use the Twilio client to send an SMS message:
$message = $client->messages->create(
'recipient_phone_number', // recipient phone number, in E.164 format
array(
'from' => 'your_twilio_phone_number',
'body' => 'Hello from Twilio!',
)
);
echo 'SMS message sent!';
PHP
Replace “your_account_sid” and “your_auth_token” with your actual Twilio account SID and auth token. Replace “recipient_phone_number” with the phone number of the recipient, in E.164 format (for example, “+1234567890”). Replace “your_twilio_phone_number” with the Twilio phone number you purchased. The “body” parameter specifies the content of the SMS message.
That’s it! Your PHP script should now be able to send SMS messages via Twilio.