Permalink
Browse files

add twilio services

  • Loading branch information...
claytantor committed Jul 21, 2012
1 parent 24e3958 commit 79c4299a12b46c2c2d14b36767695e82a353a858
Showing with 1,862 additions and 0 deletions.
  1. +17 −0 .project
  2. +166 −0 include/Services/Twilio.php
  3. +78 −0 include/Services/Twilio/ArrayDataProxy.php
  4. +78 −0 include/Services/Twilio/AutoPagingIterator.php
  5. +163 −0 include/Services/Twilio/CachingDataProxy.php
  6. +346 −0 include/Services/Twilio/Capability.php
  7. +44 −0 include/Services/Twilio/DataProxy.php
  8. +55 −0 include/Services/Twilio/InstanceResource.php
  9. +148 −0 include/Services/Twilio/ListResource.php
  10. +74 −0 include/Services/Twilio/Page.php
  11. +41 −0 include/Services/Twilio/PartialApplicationHelper.php
  12. +36 −0 include/Services/Twilio/RequestValidator.php
  13. +85 −0 include/Services/Twilio/Resource.php
  14. +26 −0 include/Services/Twilio/Rest/Account.php
  15. +10 −0 include/Services/Twilio/Rest/Accounts.php
  16. +6 −0 include/Services/Twilio/Rest/Application.php
  17. +12 −0 include/Services/Twilio/Rest/Applications.php
  18. +7 −0 include/Services/Twilio/Rest/AvailablePhoneNumber.php
  19. +30 −0 include/Services/Twilio/Rest/AvailablePhoneNumbers.php
  20. +18 −0 include/Services/Twilio/Rest/Call.php
  21. +26 −0 include/Services/Twilio/Rest/Calls.php
  22. +12 −0 include/Services/Twilio/Rest/Conference.php
  23. +6 −0 include/Services/Twilio/Rest/Conferences.php
  24. +6 −0 include/Services/Twilio/Rest/IncomingPhoneNumber.php
  25. +10 −0 include/Services/Twilio/Rest/IncomingPhoneNumbers.php
  26. +6 −0 include/Services/Twilio/Rest/Notification.php
  27. +6 −0 include/Services/Twilio/Rest/Notifications.php
  28. +6 −0 include/Services/Twilio/Rest/OutgoingCallerId.php
  29. +12 −0 include/Services/Twilio/Rest/OutgoingCallerIds.php
  30. +10 −0 include/Services/Twilio/Rest/Participant.php
  31. +6 −0 include/Services/Twilio/Rest/Participants.php
  32. +6 −0 include/Services/Twilio/Rest/Recording.php
  33. +6 −0 include/Services/Twilio/Rest/Recordings.php
  34. +6 −0 include/Services/Twilio/Rest/Sandbox.php
  35. +6 −0 include/Services/Twilio/Rest/ShortCode.php
  36. +6 −0 include/Services/Twilio/Rest/ShortCodes.php
  37. +6 −0 include/Services/Twilio/Rest/SmsMessage.php
  38. +24 −0 include/Services/Twilio/Rest/SmsMessages.php
  39. +6 −0 include/Services/Twilio/Rest/Transcription.php
  40. +6 −0 include/Services/Twilio/Rest/Transcriptions.php
  41. +25 −0 include/Services/Twilio/RestException.php
  42. +99 −0 include/Services/Twilio/TinyHttp.php
  43. +103 −0 include/Services/Twilio/Twiml.php
  44. +8 −0 index.php
  45. +9 −0 smsrequest.php
View
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>510eat</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>net.sourceforge.phpeclipse.parserbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>net.sourceforge.phpeclipse.phpnature</nature>
+ </natures>
+</projectDescription>
View
@@ -0,0 +1,166 @@
+<?php
+
+function Services_Twilio_autoload($className) {
+ if (substr($className, 0, 15) != 'Services_Twilio') {
+ return false;
+ }
+ $file = str_replace('_', '/', $className);
+ $file = str_replace('Services/', '', $file);
+ return include dirname(__FILE__) . "/$file.php";
+}
+
+spl_autoload_register('Services_Twilio_autoload');
+
+/**
+ * Twilio API client interface.
+ *
+ * @category Services
+ * @package Services_Twilio
+ * @author Neuman Vong <neuman@twilio.com>
+ * @license http://creativecommons.org/licenses/MIT/ MIT
+ * @link http://pear.php.net/package/Services_Twilio
+ */
+class Services_Twilio extends Services_Twilio_Resource
+{
+ const USER_AGENT = 'twilio-php/3.2.2';
+
+ protected $http;
+ protected $version;
+
+ /**
+ * Constructor.
+ *
+ * @param string $sid Account SID
+ * @param string $token Account auth token
+ * @param string $version API version
+ * @param Services_Twilio_Http $_http A HTTP client
+ */
+ public function __construct(
+ $sid,
+ $token,
+ $version = '2010-04-01',
+ Services_Twilio_TinyHttp $_http = null
+ ) {
+ $this->version = $version;
+ if (null === $_http) {
+ $_http = new Services_Twilio_TinyHttp(
+ "https://api.twilio.com",
+ array("curlopts" => array(CURLOPT_USERAGENT => self::USER_AGENT))
+ );
+ }
+ $_http->authenticate($sid, $token);
+ $this->http = $_http;
+ $this->accounts = new Services_Twilio_Rest_Accounts($this);
+ $this->account = $this->accounts->get($sid);
+ }
+
+ /**
+ * GET the resource at the specified path.
+ *
+ * @param string $path Path to the resource
+ * @param array $params Query string parameters
+ *
+ * @return object The object representation of the resource
+ */
+ public function retrieveData($path, array $params = array())
+ {
+ $path = "/$this->version/$path.json";
+ return empty($params)
+ ? $this->_processResponse($this->http->get($path))
+ : $this->_processResponse(
+ $this->http->get("$path?" . http_build_query($params, '', '&'))
+ );
+ }
+
+ /**
+ * DELETE the resource at the specified path.
+ *
+ * @param string $path Path to the resource
+ * @param array $params Query string parameters
+ *
+ * @return object The object representation of the resource
+ */
+ public function deleteData($path, array $params = array())
+ {
+ $path = "/$this->version/$path.json";
+ return empty($params)
+ ? $this->_processResponse($this->http->delete($path))
+ : $this->_processResponse(
+ $this->http->delete("$path?" . http_build_query($params, '', '&'))
+ );
+ }
+
+ /**
+ * POST to the resource at the specified path.
+ *
+ * @param string $path Path to the resource
+ * @param array $params Query string parameters
+ *
+ * @return object The object representation of the resource
+ */
+ public function createData($path, array $params = array())
+ {
+ $path = "/$this->version/$path.json";
+ $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
+ return empty($params)
+ ? $this->_processResponse($this->http->post($path, $headers))
+ : $this->_processResponse(
+ $this->http->post(
+ $path,
+ $headers,
+ http_build_query($params, '', '&')
+ )
+ );
+ }
+
+ /**
+ * Convert the JSON encoded resource into a PHP object.
+ *
+ * @param array $response 3-tuple containing status, headers, and body
+ *
+ * @return object PHP object decoded from JSON
+ */
+ private function _processResponse($response)
+ {
+ list($status, $headers, $body) = $response;
+ if ($status == 204) {
+ return TRUE;
+ }
+ if (empty($headers['Content-Type'])) {
+ throw new DomainException('Response header is missing Content-Type');
+ }
+ switch ($headers['Content-Type']) {
+ case 'application/json':
+ return $this->_processJsonResponse($status, $headers, $body);
+ break;
+ case 'text/xml':
+ return $this->_processXmlResponse($status, $headers, $body);
+ break;
+ }
+ throw new DomainException(
+ 'Unexpected content type: ' . $headers['Content-Type']);
+ }
+
+ private function _processJsonResponse($status, $headers, $body) {
+ $decoded = json_decode($body);
+ if (200 <= $status && $status < 300) {
+ return $decoded;
+ }
+ throw new Services_Twilio_RestException(
+ (int)$decoded->status,
+ $decoded->message,
+ isset($decoded->code) ? $decoded->code : null,
+ isset($decoded->more_info) ? $decoded->more_info : null
+ );
+ }
+
+ private function _processXmlResponse($status, $headers, $body) {
+ $decoded = simplexml_load_string($body);
+ throw new Services_Twilio_RestException(
+ (int)$decoded->Status,
+ (string)$decoded->Message,
+ (string)$decoded->Code,
+ (string)$decoded->MoreInfo
+ );
+ }
+}
@@ -0,0 +1,78 @@
+<?php
+
+/**
+ * DataProxy implementation using an array for storage backend.
+ *
+ * @category Services
+ * @package Services_Twilio
+ * @author Neuman Vong <neuman@twilio.com>
+ * @license http://creativecommons.org/licenses/MIT/ MIT
+ * @link http://pear.php.net/package/Services_Twilio
+ */
+class Services_Twilio_ArrayDataProxy
+ implements Services_Twilio_DataProxy
+{
+ protected $array;
+
+ /**
+ * Constructor.
+ *
+ * @param array $array Array representation
+ */
+ public function __construct($array)
+ {
+ $this->array = $array;
+ }
+
+ /**
+ * Implementation of retrieveData.
+ *
+ * @param string $key The index
+ * @param array $params Optional parameters
+ *
+ * @return object Object representation
+ */
+ function retrieveData($key, array $params = array())
+ {
+ return (object) $this->array;
+ }
+
+ /**
+ * Implementation of createData.
+ *
+ * @param string $key The index
+ * @param array $params Optional parameters
+ *
+ * @return object Object representation
+ */
+ function createData($key, array $params = array())
+ {
+ return (object) $this->array;
+ }
+
+ /**
+ * Implementation of updateData.
+ *
+ * @param array $params Update parameters
+ *
+ * @return object Object representation
+ */
+ function updateData(array $params)
+ {
+ return $this->array;
+ }
+
+ /**
+ * Implementation of magic method __get.
+ *
+ * @param string $prop The name of the property to get
+ *
+ * @return mixed The value of the property
+ */
+ function __get($prop)
+ {
+ return is_array($this->array)
+ ? $this->array['prop']
+ : $this->array->$prop;
+ }
+}
@@ -0,0 +1,78 @@
+<?php
+
+class Services_Twilio_AutoPagingIterator
+ implements Iterator
+{
+ protected $generator;
+ protected $promoter;
+ protected $args;
+ protected $items;
+
+ private $_args;
+
+ public function __construct($generator, $promoter, array $args) {
+ $this->generator = $generator;
+ $this->promoter = $promoter;
+ $this->args = $args;
+ $this->items = array();
+
+ // Save a backup for rewind()
+ $this->_args = $args;
+ }
+
+ public function current()
+ {
+ return current($this->items);
+ }
+
+ public function key()
+ {
+ return key($this->items);
+ }
+
+ public function next()
+ {
+ try {
+ $this->loadIfNecessary();
+ return next($this->items);
+ }
+ catch (Services_Twilio_RestException $e) {
+ // Swallow the out-of-range error
+ }
+ }
+
+ public function rewind()
+ {
+ $this->args = $this->_args;
+ $this->items = array();
+ }
+
+ public function count()
+ {
+ throw new BadMethodCallException('Not allowed');
+ }
+
+ public function valid()
+ {
+ try {
+ $this->loadIfNecessary();
+ return key($this->items) !== null;
+ }
+ catch (Services_Twilio_RestException $e) {
+ // Swallow the out-of-range error
+ }
+ return false;
+ }
+
+ protected function loadIfNecessary()
+ {
+ if (// Empty because it's the first time or last page was empty
+ empty($this->items)
+ // null key when the items list is iterated over completely
+ || key($this->items) === null
+ ) {
+ $this->items = call_user_func_array($this->generator, $this->args);
+ $this->args = call_user_func_array($this->promoter, $this->args);
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 79c4299

Please sign in to comment.