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
- Respond to Incoming Phone Calls in PHP
- Browser Calls with PHP and Laravel
- Call My Nana: Danielle Leong Sets Up Life Saving Voice Reminders For Her Grandma With Twilio
- Build Your Own Agent SMS Chat Using Twilio Toll-free Numbers
- Victory 91.5 Takes Radio From Hardware Powered to Software Powered with Twilio