In Thoughts

Basic PHP integration with Twilio for sending SMS text messages

Sending an SMS text via PHP can be a tricky task, especially in the UK. The often suggested and most cost-effective way to do it is sending an email to a network providers SMS gateway email address and letting them convert the message to text for you. There are two key issues with this:

1. You have to first determine the network you’re sending the message to.

This isn’t a show stopper if you have the data to hand to begin with but staying on top of this or having to ask for this from whomever you’re intending to send the message to isn’t ideal.

2. Some UK networks just flat out don’t provide a public gateway.

EE for example don’t have a gateway and don’t seem to be keen to provide one. This means even if you have the network information it’s going to be hit and miss on the gateway front. 

Given these issues when we were recently tasked with sending a text to a number collected via a form we opted to use the Twilio API as a wrapper to handle all the SMS functionality. Offsetting the cost of sending these messages via Twilio against the maintenance of various network gateways and the aforementioned issues this seemed a no-brainer for us.

So here is how we did it. The task – to receive a mobile telephone number – send a text message to this number and handle any response to the text via call or message.

1. Setup Twilio

Signing up for an account with Twilio is easy enough. You’ll be assigned a number which you’ll use to send your messages. This is just a test number until you purchase it which we did straight away as explained in the next step.

2. Buy a number

You’ll be assigned a number when you sign up to Twilio that you can use for testing but can’t send any actual texts through it. We just credited the account straight away so we could see the text coming through live. For us and probably most others we only needed to send a handful of texts whilst testing. As you’ll see further down this write up, the library provided by Twilio is super simple and you are only charged when a text is successfully delivered so for the cost of having the convenience of actual messages being delivered to real phones was well worth it.

3. Grab the PHP library and your API credentials

Download the PHP library provided by Twilio and put it somewhere that it can be included in the PHP script you want to use to send the SMS text. When sending your messages you’ll also need your account sid and your Auth Token. These can be grabbed from your account settings.

4. Set up the sending of the message

Firstly include the PHP library you’ve just downloaded and unzipped:

require “PATH YOUR TWILIO LIBRARY/Services/Twilio.php”;

Instantiate a new instance of the Twilio service with the Account sid and Auth Token collected earlier:

$client = new Services_Twilio(‘ACCOUNT SID HERE’, ‘AUTHTOKENHERE’);

Send a new SMS text message, replace the lines in capitals below with information relevant to your account and of course the number you are sending the message to:

$client->account->messages->create(array(
‘To’ => ‘NUMBER OF RECIPIENT’,
‘From’ => ‘NUMBER PURCHASED WITH YOUR ACCOUNT’,
‘Body’ => “THIS IS YOUR TEXT MESSAGE”,
));

It’s as easy as that! You’ll now be sending live text messages via PHP and Twilio. You can keep an eye on the outgoing messages via the logs area of your Twilio account area and keep an eye on costs via the usage area.

5. Handle Responses

Now that texts are being sent what happens if whoever receives the texts replies either by calling the number or texting back? Twilio allows you to handle the responses in a number of ways. If you go into the main configuration page for your purchased number you can see two sections voice & messaging.

Voice

There are a number of ways to handle a person calling the number you have just purchased and sent a text from. You can see more information on the options here. These options include setting up voice responses using xml amongst others. As well as building your own response you can use predefined responses from Twilio in the form of twimlets. We just went with good old fashioned forwarding, provided by twimlets so that any calls can be answered by an actual person on the clients end. To do this you’ll need to set the voice option to ‘configure with’ url. In the request url you can use the following:

http://twimlets.com/forward?PhoneNumber=YOURFORWARDEDNUMBERHERE&

or you can generate your own url using the twimlet generator here.

Messaging

Dealing with a text response is much simpler than a voice response. You can find more information replying to a text here. Essentially you’ll be giving Twilio a url to make a request to in the same way we did with the voice response but this will be on your server, a live server that can handle the request. The response from that url will determine what Twilio does next. All responses are also kept within the logs area of your account pages on Twilio. Here’s an example of file we created using XML that sent a further response any texts that were received at the Twilio number.

if($_SERVER[‘REQUEST_METHOD’] != ‘POST’){
die;
}

$subject = ‘Incoming Text Message From xxxxx’;
$message = “From : ” . $_POST[‘From’] . “\r\n” . $_POST[‘Body’];
mail(‘ouremail@ourdomain.com’, $subject, $message);

header(‘Content-Type: text/plain’);
echo ‘Thanks for your reply, please direct any further queries to support@ourdomain.com’;

When Twilio sends the request to the url you have configured in the number configuration screen it does so by posting to it. This means all the information it posts will be accessible via:

$_POST[‘informationkey’]

In the example above, we first check that something is posting to the page before sending an email to ourselves to let us know that an incoming text has been received. Following on from that we set the header type text/plain and simply output what we’d like to be text back to the number that has sent the text, Twilio does the rest. In this case we wanted to encourage people to move there query over to email.

That’s sending a text and handling a response via text or voice all wrapped up in a small amount of code. Be sure to read through the Twilio Docs. for more information on building out a more powerful SMS message service.

Web Design Tools

Test your skills with our tools