Install the Twilio PHP Helper Library Without Composer

November 21, 2019
Written by
Paul Kamp
Twilion

php-helper-library-no-composer-header

PHP is a venerable language. As I go to press, it’s in major version 7 and has maintained it’s well-deserved popularity for nearly 25 years.

With two and a half decades of evolution, we PHP developers have quite a few ways to do things. To wit: many of you ask us how to use Twilio’s PHP Helper Library directly, without a package manager.

Today, I’ll show you how to install Twilio’s PHP Helper Library on *NIX or Mac OSX without using the package manager Composer.

Let’s compose some magic.

Prerequisites to Install The Twilio PHP Helper Library (Without Composer)

Before we get started with the install, you’ll need to set a few things up, confirm a few others, and find a phone:

Once we’re on the same page (pun intended!), move onto the next step.

Install The Twilio PHP Helper Library without Composer

If you’ve done the above steps and logged into Twilio, you’re ready to start installing the Helper Library.

Download and Decompress the Twilio PHP Helper Library

In your favorite console, create a new directory for your project and change into it:

mkdir new-twilio-project
cd new-twilio-project

Download the PHP Helper Library from the Helper Library page, and unzip it inside. You can also grab the latest library from Github and unzip it from the command line:

wget https://github.com/twilio/twilio-php/archive/master.zip
unzip master.zip

Test the Helper Library

Now that you have the PHP Helper library downloaded and unzipped, we’re going to test your install by sending an SMS.

You’re going to need a phone number before we can test sending an SMS. If you already have a Twilio-purchased phone number to use to test, skip the next section.

Purchase a Twilio phone number (if you don’t have one)

Navigate to the Buy a Number page in the Twilio Phone Numbers console.

Check the ‘SMS’ box, then search for your choice of phone number.

'SMS' box in phone number search

Finally, ‘Buy’ your favorite.

How to buy a Twilio phone number

For many countries and territories, you'll need to fulfill regulatory requirements before you purchase your first number. See our Phone Number Regulatory Requirements page.

Write PHP to Send an SMS

Create a new file – name it whatever you’d like with the extension .php– then open it in your favorite text editor:

touch whatever-you-like.php
ed whatever-you-like.php 
# or, you know, a different text editor.

Now, open the Twilio console. You need to prepare to make some substitutions to the code I provide.

Make a note of your Account SID and Auth Token (Click ‘View’ to see the token.)

Where to find Account SID and Auth Token in the Twilio console

Now, in your text editor, paste the following code:

<?php
require __DIR__ . '/twilio-php-master/src/Twilio/autoload.php';
use Twilio\Rest\Client;
// In production, these should be environment variables.
$account_sid = 'Put Account SID Here';
$auth_token = 'Put Auth Token Here';
$twilio_number = "+18005550000"; // Twilio number you own
$client = new Client($account_sid, $auth_token);
// Below, substitute your cell phone
$client->messages->create(
    '+18005551212',  
    [
        'from' => $twilio_number,
        'body' => 'This message is Un-Composed'
    ] 
);

Insert your Account SID and Auth Token (from the previous step) into the code where $account_sid and $auth_token appear, respectively.

While fine for a test, in production you should load your credentials as environment variables. With this setup, it’s unfortunately very easy to check them into a repository for others to see.

Substitute the value of $twilio_number with a Twilio number you own (or the number you just purchased). Use E.164 format here – you can find it in an easy, copy/paste-able format in the Phone Number console.

Finally, substitute your cell phone’s number into the line below $client->messages->create(, where I currently have +18005551212. Again, use E.164 format.

Test the SMS PHP Script

Save your file and go back to the command line. We’re almost ready for some magic... make sure your phone is on and by your side.

In your console, run the file:

php whatever-you-like.php

Boom! Did you get the text?

Installing the PHP Helper Library Without Using Composer

And there you have it – the humble beginnings of your next project, all without the use of Composer.

Now – I certainly recommend you use a package manager of some sort for your complex communications applications. In fact, I wrote our PHP SMS Quickstart with Composer in mind – read it if you’d like to try it out.

But if you just don’t want to take this particular plunge, at least you’ve got the scaffolding of a Composer-less app. And whether you use a package manager or not, we can’t wait to see what you build.

Additional Resources:

Paul Kamp is Editor-in-Chief of the Twilio Blog. When not using Composer, he usually can be found writing Python. He can be reached at pkamp [at] twilio.com.