Using Twilio for SMS Text Messaging

Want to send SMS messages to your PURL visitors? Twilio makes this extremely easy! Follow the steps below to integrate SMS text messages into your PURL landing pages:

1. Create a Twilio Account

Twilio has a free plan that you can sign up for here.

2. Install Twilio's PHP Helper Library

Click here to download the twilio-php source code (.zip) which includes all dependencies.

Once you download the library, unzip and move the twilio-php folder to your landing page folder. This is the folder that was installed when you created your Purlem campaign. The directory will start withpurlpage_ and end with the name of your campaign. 

Finally, upload the twilio-php folder to your server.

3. Add the Twilio code to the Landing Page file

The landing page file is the index.php file that is located in your purlpage_ folder. (Same folder from Step 2). Under where it say END PURL CODE, add the following:

if($_GET['page'] == 100) {
require 'twilio-php/Services/Twilio.php';

// set your AccountSid and AuthToken from
$AccountSid = "xxxxxxxxxxxxxxxxxxxxxx";
$AuthToken = "xxxxxxxxxxxxxxxxxxxxxx";

$client = new Services_Twilio($AccountSid, $AuthToken);

$sms = $client->account->sms_messages->create(
"xxxxxxxxxxx", // From this number
$visitor->phone, // To this number
	"Hey ".$visitor->firstName.", 
	Thanks for visiting your PURL!"

Let's pull apart this code a bit so you can see what is going on.  

  • In the first line, if($_GET['page'] == 100) { is telling us to send the SMS message when the thank you page is visited.  100 = the thank you page.  If you wanted the email to trigger when the first page was visited, you would use  if($_GET['page'] == 100) { instead.
  • require 'twilio-php/Services/Twilio.php'; is referencing Twilio's PHP library uploaded from Step 2. 
  • $AccountSid = "xxxxxxxxxxxxxxxxxxxxxx"; and $AuthToken = "xxxxxxxxxxxxxxxxxxxxxx"; will need to be replaced with your Twilio AccountSid and AuthToken.  You can find both of these by logging into your Twilio account. 
  • "xxxxxxxxxxx", // From this number needs to be replaced with your Twilio SMS number
  • $visitor->phone, // To this number is requesting the visitor's phone number, and will be the number used to send the SMS message to. You don't need to update this at all.
  • "Hey ".$visitor->firstName.", Thanks for visiting your PURL!" is the actual content of the SMS message. You can change this to whatever message you want to be sent to the visitor. 

Save and upload the changes made to the landing page file. 

4. Finish & Test

If everything was put together correctly, when you visit your PURL, complete the form, and are taken to the Thank You page, an SMS message will automatically be sent to your PURL.

Note: If your using Twilio's free account, they will only allow you to send messages to your phone number (used when creating your Twilio account).  So if your wondering why the messages are not being sent to other phone numbers, that could be why.  You may need to upgrade your Twilio account to send to other numbers.