It takes just a few lines of code and even fewer minutes to send your first text message with PHP and Twilio. Here’s how:
Sign up for a free Twilio account and buy a phone number.
Install the PHP helper library with Composer: composer require twilio/sdk:5.* then put require "vendor/autoload.php'; at the top of your file.
Find your account SID and auth token in your console dashboard, and your phone number in your phone numbers list.
Create a file called send-sms.php and paste in the code below to:
- Include the helper library
- Create a Twilio client using our account credentials
- Create a new message from our Twilio number, to our cellphone
<?php
require_once "vendor/autoload.php";
use Twilio\Rest\Client;
$account_sid = "YOUR_TWILIO_ACCOUNT_SID";
$auth_token = "YOUR_TWILIO_AUTH_TOKEN";
$twilio_phone_number = "YOUR_TWILIO_PHONE_NUMBER";
$client = new Client($account_sid, $auth_token);
$client->messages->create(
'DESTINATION_PHONE_NUMBER',
array(
"from" => $twilio_phone_number,
"body" => "Whaddup from PHP!"
)
);
Save your file and run it:
php send-sms.php
Boom! Your phone should light up with a text!
Next Steps
We’re just scratching the surface of what’s possible with PHP and Twilio. If you’d like to explore more, check these out:
- The Twilio PHP documentation
- The Twilio PHP quickstarts
- How to build an SMS trivia game in ~40 lines of PHP
- Call Tracking in PHP
- Getting started with Twilio and Laravel
- The production ready Twilio powered PHP apps over in the tutorials
If you build something sweet using PHP and Twilio, or if you have any questions, I’d love to hear about it. Please drop me a line at @greggyb or gb@twilio.com.