Getting Started with Twilio and Google App Engine for PHP

Today’s post comes from Keith Casey, Sr. Developer Evangelist at Twilio. In this post, Keith describes how to use Twilio with Google App Engine on the PHP runtime. You can follow Keith on Twitter at @caseysoftware or on Google+.



I’ve wanted to explore Google App Engine for years. Between its SLA, automatic scaling, and queuing system, it has always been compelling. Unfortunately, since my Python skills are somewhere between “Hello World” and “OMG What did I just do!?” I decided to save myself the embarrassment. When Google announced PHP support for App Engine, I was both ecstatic and intrigued about what might be possible. To get something running in just a few minutes, I decided to use our Twilio PHP helper.



When experimenting with a new Platform as a Service, there are nuances of which you should be aware like dealing with a virtualized file system and needing a separate service for email. However, the remaining nuances are usually pretty minimal and required only the “tweaking of my module” rather than a heavy “rebuilding of my app”.



Knowing that up front, let’s dig in.



Set up the PHP on App Engine environment



First, check out and follow Google’s Getting started with PHP on Google App Engine to set up your local environment. Their instructions will cover setting up the SDK, connecting to your account, and some details on debugging. It should set up your environment under http://localhost:8080/ which serves as the root of your application.



Upgrade your Twilio Helper library



Next, with respect to Twilio’s PHP Helper library, we’ve taken care of the nuances for you, the most important one of which involved falling back to PHP’s Streams when cUrl isn’t available. In your case, simply upgrade the library to v3.11+ or install it for the first time. You can use the library to send text messages and make phone calls exactly as you would in any other PHP environment:



<?php
// Include the Twilio PHP library
require "Services/Twilio.php";

// Set your AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";

// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);

// Make an array of people we know, to send them a message:
$people = array(
"+14158675309" => "Curious George",
"+14158675310" => "Boots",
"+14158675311" => "Virgil",
);

// Loop over all our friends:
foreach ($people as $number => $name) {

$sms = $client->account->sms_messages->create(
"YYY-YYY-YYYY", // Change the From number to a Twilio number youve purchased
$number, // the number we are sending to - Any phone number
"Hey $name, Monkey Party at 6PM. Bring Bananas!" // the sms body
);

echo "Sent message to $name"; // Display a confirmation message
}


If you’re interested in the the specific changes for our library, you can explore the relevant pull requests here and here. I also have an article called “Preparing your PHP for App Engine” in next month’s php|architect magazine.



URL Routing in PHP



Within App Engine, the entire routing system is powered by the app.yaml file. If you’re familiar with development using frameworks like Zend or Symfony, defining your routes will come naturally and may be marginally more difficult than copy/paste. If you’re only familiar with doing non-framework development in PHP, you’ll have to define a route for each and every PHP file the user accesses. In Google’s example “hello world” app, your app.yaml file should should begin with something like this:



application: examplehelloworld
version: 1
runtime: php
api_version: 1
threadsafe: true

handlers:
- url: .*
script: main.php


The request handlers are the important part. If you’re using an MVC framework or OpenVBX, you’ll most likely have one primary element mapping your index.php file and a set of elements for static assets like JavaScript, CSS, and images.



Alternatively, if you’re not using a using a framework, app.yaml will map different URL patterns to individual PHP scripts, like this:

application: examplehelloworld
version: 1
runtime: php
api_version: 1
threadsafe: true

handlers:
- url: /images
static_dir: images

- url: /send-sms
script: send-sms.php

- url: /make-call
script: make-call.php

- url: .*
script: index.php


Within those individual PHP scripts, no changes are necessary, so our make-call.php is straightforward and identical to our quickstart:

<?php

require Services/Twilio.php;
include credentials.php;

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

$call = $client->account->calls->create(
1512-555-1212, // From this number
17035551212, // Send to this number
http://twimlets.com/echo?Twiml=%3CResponse%3E%3CSay%3EHello%20Monkey!%3C%2FSay%3E%3C%2FResponse%3E&
);
print $call->sid;


Also, the send-sms.php can be the same as our normal quickstart:

<?php

require Services/Twilio.php;
include credentials.php;

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

$call = $client->account->sms_messages->create(
1512-555-1212, // From this number
17035551212, // Send to this number
Hello monkey!!
);
print $call->sid;


Now you can access your scripts via http://localhost:8080/make-call.php and http://localhost:8080/send-sms.php respectively. If the file is one that doesn’t need to be accessible publicly, such as your credentials.php file, it shouldn’t have a listing.



Voila. You now have your first PHP application on Google App Engine!



I hope this is helpful as you test and deploy your PHP applications on Google App Engine. If you have any tips, feedback, suggestions, or just a good joke please let me know via email or Twitter: @CaseySoftware.



-Contributed by Keith Casey, Sr Developer Evangelist, Twilio

Related Posts by Categories

0 komentar:

Posting Komentar


Blog Archive

Diberdayakan oleh Blogger.