Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Twilio & The Zend Framework - TwilioCon 2011

Twilio integrates well with MVC frameworks - and the Zend Framework is no exception.

Tim Lytle

on 1 April 2012

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Twilio & The Zend Framework - TwilioCon 2011

$response = new Services_Twilio_Twiml();
$response->say('Hello from Zend.');
echo $response;
Tim Lytle
Contract Developer
Zend CE
@tjlytle The Zend Framework Solid MVC Framework for PHP
Pick and Choose Component Library Zend_Service_Twitter
Zend_Db Twilio & MVC <form action='query.php'>
Order Number:
<input type='text' name='ordernum'>
<input type='submit' value='Search'>
</form> <Gather action='query.php'>
<Say>Enter order number.</Say>
</Gather> TwiML & HTML $order = $user->orders->new();
$order->date = new DateTime('today');

$id = $order->getId(); $response = $client->request(
'From' => '215-555-3400',
'To' => '415-555-1212',
'Url' => 'http://demo.twilio.com/welcome'

$sid = $response->ResponseXml->Call->Sid; $call = $twilio->account->calls->create(
'9991231234', // From this number
'8881231234', // Call this number

$sid = $call->sid; In case you only recognize people by their profile pics. API & The Model Model - REST API
View - TwiML
Controller Okay, so it's
not pretty. Not so ugly. Get Phone Service Pick Up The Phone + protected function _initTwilio(){
$config = $this->getOption('twilio');
$twilio = new Services_Twilio($config['sid'], $config['token']);
return $twilio;
} Service Library TwiML Keep things simple by extending the Controller or adding an Action Helper that gets the Twilio service, and configures the view. MVC Structure /application
/library /forms
/services Not created by default,
but can be added: Good place for the Twilio helper library, this is on the include path. application.ini contains the general configuration. Custom bootstrap code is here. $zf create project ./ Bootstrap.php calls all funtions starting with _init. The return is stored in the bootstrap object and accessed using getResource(). Get the 'twilio' array from application.ini. Configuration autoloadernamespaces[] = Services
twilio.sid = ACba8bc05eacf94afdae398e642c9cc32d
twilio.token = xxxxx Autoload classes in the 'services' directory, by adding that path to the namespaces array. Access Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('twilio');
$this->getInvokeArg('bootstrap')->getResource('twilio'); The return of _initTwilio(). Don't like any of these?
You can do this. Zend_Registry::set('twilio', $twilio);
Zend_Registry::get('twilio'); In _initTwilio(). Anywhere else. views/scripts/[controller]/[action].phtml Path to the
view scripts. Generate TwiML Autoloading enabled in the configuration. Output Plain TwiML <Response>
<Say>Hello from Zend.</Say>
</Response> This is what I do. Set the Content-Type & Disable Layouts $this->getResponse()->setHeader('Content-Type', 'application/xml'); $this->getHelper('layout')->disableLayout(true); The response object also has things like response codes and the body. The layout helper is only available if layouts are enabled. No reason to use the MVC core if you don't need it. Sponsored by Zend, but community driven. Well tested,
'enterprise' ready. Regular update cycle. Easily use and extend the base classes. Basic Request Bootstrapping
Twilio The
Basics An IVR Menu Starting A Call public function welcomeAction(){}; public function messageAction(){
$this->session->message = $this->_getParam('RecordingUrl');
} <Say>Hello.</Say>
<?php echo $this->url(array('action' => 'message'));
</Redirect> Keep every action simple. Even if it just renders a view. Use the 'url' helper in the view to
direct the call to the next action. Collect data using Zend_Session_Namesapce(). Use the 'redirector' helper in the controller to
direct the call to the next action. $url = $this->getHelper('url')->simple('welcome'); Get a url for the starting action
using the 'url' helper. For some reason, this is *not* documented in the reference manual. $url = 'http://' . $_SERVER['HTTP_HOST'] . $url; 'url' helper doesn't include the host or protocol. $this->twilio->account->calls->create($from,
$params); The twilio service can be retrieved from the bootstrap object, or Zend_Registry. Text TWL55 to 46876 (INTRO) Comments? Follow up? Want a copy of this stuff? Start A Party Line Integrate Existing Applications ContextSwitch Modules Tools to http://example.com/[controller]/[action] application/controllers/IndexController.php public function indexAction(){} http://example.com/order/search application/controllers/OrderController.php public function searchAction(){} $contextSwitch->addContext('twiml', array('suffix' => 'twiml'));
$contextSwitch->addHeader('twiml', 'Content-Type', 'application/xml');
$contextSwitch->addActionContext('search', 'twiml');
$contextSwitch->initContext(); http://example.com/order/search?format=twiml order/search.twiml.phtml The 'contextSwitch' action helper modifies the view based on a format parameter. The context name. Suffix for view script. Context specific header. Enable the context for an action. Enable the helper. By default, any context disables the layout. $this->getHelper('contextSwitch'); Get helper from controller. public function searchAction(){
$this->view->order = $order;
} Normal requests render the HTML view. 'twiml' requests
render the TwiML view. Both use the same data. <Redirect>
</Redirect> Use '<Redirect>' to serve TwiML
to other applications, keeping the menu there. /application
/views So you have Want
to Or maybe An application can be organized into 'modules'. Each 'module'
has an application like structure. 'Twilio_IndexController' Make this even better by extending the 'contextSwitch' helper to use the 'twiml' context when the request comes from Twilio's servers. Starting with First Step For example... But it's not in PHP... Write a quick script... Using some
Zend components... This may be a useful tool... Convert
to MVC. Just the default route. Regex, module, REST routes can be used, or any custom route object. application/views/scripts/order/search.phtml Will render this as the view by default. Controller. Action. No template 'language', just PHP. Create calls. Search through recordings. Provision & release numbers. Communicate
data. Request input. The application. You could also just define an ApplicationSid in the configuration file. SimpleXML cURL Developers
Full transcript