Next Generation PHP Helper Library Release

September 07, 2016
Written by

Nextgen PHP libraries

Today, we are pleased to announce the general availability of our next generation PHP helper library. The library gives you new functionality and takes advantage of modern PHP language features. We’ve rebuilt it from the ground up based on the developer feedback we received from you.

What’s new

Earlier this year we introduced the new Twilio Helper Libraries and provided some insights into Twilio’s effort to auto-generate all of our helper libraries, using an in-house framework. The goal was to make the helper-libraries more consistent, enable faster iteration, and improve the libraries. The foundation is shared across languages, which provides greater consistency and speeds up feature delivery and bug fixes. Features added or bugs fixed in the core framework will be available for all new helper libraries, at the same time.

Back in 2011, we launched the current generation of helper-libraries. PHP has evolved a lot since then, and our next generation helper library should reflect that evolution. We updated how arguments are passed into methods. The new version expects required parameter of a method as individual arguments, all other parameters will be passed in via an associative array. This makes the resulting code easier to read and enables the library to catch missing parameters before the HTTP request is made. With the new approach the library only creates a request if all the required parameters are present.

$message = $client->account->messages->create(
   // the number we are sending to
   $number,

   array(
       'from' => "YYY-YYY-YYYY",
       'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!",
       'mediaUrl' => array("https://demo.twilio.com/owl.png",
           "https://demo.twilio.com/logo.png")
   )
);

Using namespaces and improving how our classes are commented has also drastically improved the experience in your IDE – here’s an example of a much more helpful auto-complete experience in PHPStorm:

Screen Shot 2016-09-01 at 6.17.54 PM

Learning from your feedback, we iterated on what works well and improved where we could do better. Two of the asks that we got frequently were to add better paging and namespace support. Both are added to the new libraries.

The new PHP helper library is now officially supported and ready for production use cases.

How to get started

The best place to start is the new Getting Started Guide that covers installation and how to send your first message.

Our Quickstart now provide code samples for the new and old libraries. You can toggle between versions in the top right corner of the code boxes to see the difference between versions.

Another great resource is the Migration Guide, especially if you are familiar with the previous generation. It covers some of the new features and highlights the differences.

The new libraries come in two lines. The ’mainline’ that contains our GA products and is the most stable release, and ‘edge’ which includes new features that are in Developer Preview or Beta. Here is a more detailed writeup about our Versioning Strategy.

Let’s look at some code

Namespaces

The new SDK takes advantage of PHP namespaces for proper autoloading. You will be able to use namespaces as in the following example.

<?php
// Composer autoload file
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;

Accessing REST Resources

The new helper library improved network efficiency therefore it introduces two new concepts.

  • Resource Context: A reference to a resource in the REST API that has not yet been operated on or fetched
  • Resource Instance: A representation of a resource that has been retrieved from the API after some operation has been performed

<?php
require '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;

// Create REST API Client
$accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$client = new Client($accountSid, $token);

// 1.) Message instance context
$messageContext = $client->messages('SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

// 2.) Message instance 
$message = $messageContext->fetch();
echo $message->body;

The HTTP only happens once fetch() is called.

Listing and Paging Resources

Getting lists of resources from the API follows a similar pattern. To page through larger result sets, you can use the “stream” method, which returns an iterator that will transparently loop through all the possible results for your resource context.

<?php
require '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;

// Create REST API Client
$accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$client = new Client($accountSid, $token);

$messagesStream = $client->messages
   ->stream();

// List ALL the messages
foreach ($messagesStream as $message) {
   echo $message->body;
}

The Migration Guide provides a much more detailed overview.

Deprecation

Going forward, new functionality will only be added to the new libraries (PHP 5.x). The old libraries (PHP 4.x) will be officially supported until 11/30/16. After that day, Twilio will stop providing bug fixes and Support might ask you to upgrade before debugging issues.

Next-gen Release Candidates

We’d love your get your feedback on our other release candidates. They are available on Github now and we are interested in your issue tickets and pull requests.

The next language that we will release is Java. Stay tuned!