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.
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:
Boom! Your phone should light up with a text!
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
- ETA Notifications with PHP and Laravel
- Secure your PHP/Lumen app by validating incoming Twilio requests
- Build Your Own Agent SMS Chat Using Twilio Toll-free Numbers
- How to Build a Digital Decoder Ring Using PHP, FilePreviews.io and Twilio MMS
- Round up: Libraries for checking Pwned Passwords in your 7 favorite languages