10 Jan 2011

Hacking Twilio's PHP library II

So here are three more hacks for the Twilio PHP library.

1. Support for both XML and JSON

In my last piece, I posted a code hack that enables using the JSON endpoints of the API instead of the XML. What if we can find a way to support both? Here is a rewrite of the TwilioRestResponse constructor class to do that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
// ...
public function __construct($url, $text, $status) {
    preg_match('/([^?]+)\??(.*)/', $url, $matches);
    $this->Url = $matches[1];
    $this->QueryString = $matches[2];
    $this->ResponseText = $text;
    $this->HttpStatus = $status;
    
    // Lets check for XML first
    // ...easier to detect that Json
    if($this->HttpStatus != 204)
        $this->Response = @simplexml_load_string($text);
    
    // If it fails, then prolly json
    if ($this->Response === FALSE) {
        $this->Response = @json_decode($text);
        $isJson = true;
    }
    
    if($this->IsError = ($status >= 400))
        $this->ErrorMessage =
            $isJson ? (string) $this->Response->message : (string) $this->Response->RestException->Message;
    
}

Notice that the result of your API is now available via $this->Response and not $this->ResponseJson or $this->ResponseXML. You will also notice I checked for XML first. For some funny reasons, trying to json_decode an XML string gives a valid result but parsing a JSON string with simplexml fails. So that explains why.

2. Support for Reject

Trying to construct a Reject response gives an error with the library. In other words, the following code will fail:

1
2
3
4
<?php
// ...
$r = new Response();
$r->append(new Reject());

The simple workaround is including 'Reject' in the nesting array of the Response class:

1
2
3
4
5
6
<?php
// ...
class Response extends Verb {
    private $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response></Response>";
    protected $nesting = array('Say', 'Play', 'Gather', 'Record', 
        'Dial', 'Redirect', 'Pause', 'Hangup', 'Sms', 'Reject');

3. Recording DAILed calls

Although there is no official documentation of how to record a conversation instatiated with the DIAL verb, there is a workaround. Problem is, adding the record attribute fails with the library. The hack is to add 'record' to the valid array of the Dial class

1
2
3
4
5
<?php
// ...
class Dial extends Verb {
    protected $valid = array('action','method','timeout','hangupOnStar',
        'timeLimit','callerId','record');

Please note that recording Dailed calls is not officially supported by the API and this feat may change anytime.

You can download the whole new source code of the Twilio class here: http://cl.ly/3wyA

By the way, wanna check out the web app I built with the API? Here: goAhoy.com. I'm sending out invites already, so don't forget to drop your number on the signup page.

 

Looking for a simple marketing automation tool to automate your customer onboarding, retention and lifecycle emails? Check out Engage and signup for free.

 

My name is Opeyemi Obembe. I build things for web and mobile and write about my experiments. Follow me on Twitter—@kehers.

 

Next post: Twilio's PHP library support for JSON