123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?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');
- class Services_Twilio extends Services_Twilio_Resource
- {
- const USER_AGENT = 'twilio-php/3.12.8';
- protected $http;
- protected $retryAttempts;
- protected $last_response;
- protected $version;
- protected $versions = array('2008-08-01', '2010-04-01');
- public function __construct(
- $sid,
- $token,
- $version = null,
- Services_Twilio_TinyHttp $_http = null,
- $retryAttempts = 1
- ) {
- $this->version = in_array($version, $this->versions) ?
- $version : end($this->versions);
- if (null === $_http) {
- if (!in_array('openssl', get_loaded_extensions())) {
- throw new Services_Twilio_HttpException("The OpenSSL extension is required but not currently enabled. For more information, see http://php.net/manual/en/book.openssl.php");
- }
- if (in_array('curl', get_loaded_extensions())) {
- $_http = new Services_Twilio_TinyHttp(
- "https://api.twilio.com",
- array(
- "curlopts" => array(
- CURLOPT_USERAGENT => self::qualifiedUserAgent(phpversion()),
- CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
- ),
- )
- );
- } else {
- $_http = new Services_Twilio_HttpStream(
- "https://api.twilio.com",
- array(
- "http_options" => array(
- "http" => array(
- "user_agent" => self::qualifiedUserAgent(phpversion()),
- "header" => "Accept-Charset: utf-8\r\n",
- ),
- "ssl" => array(
- 'verify_peer' => true,
- 'verify_depth' => 5,
- ),
- ),
- )
- );
- }
- }
- $_http->authenticate($sid, $token);
- $this->http = $_http;
- $this->accounts = new Services_Twilio_Rest_Accounts($this, "/{$this->version}/Accounts");
- $this->account = $this->accounts->get($sid);
- $this->retryAttempts = $retryAttempts;
- }
-
- public static function qualifiedUserAgent($php_version) {
- return self::USER_AGENT . " (php $php_version)";
- }
-
- public function getVersion() {
- return $this->version;
- }
-
- public function getRetryAttempts() {
- return $this->retryAttempts;
- }
-
- public static function getRequestUri($path, $params, $full_uri = false) {
- $json_path = $full_uri ? $path : "$path.json";
- if (!$full_uri && !empty($params)) {
- $query_path = $json_path . '?' . http_build_query($params, '', '&');
- } else {
- $query_path = $json_path;
- }
- return $query_path;
- }
-
- protected function _makeIdempotentRequest($callable, $uri, $retriesLeft) {
- $response = call_user_func_array($callable, array($uri));
- list($status, $headers, $body) = $response;
- if ($status >= 500 && $retriesLeft > 0) {
- return $this->_makeIdempotentRequest($callable, $uri, $retriesLeft - 1);
- } else {
- return $this->_processResponse($response);
- }
- }
-
- public function retrieveData($path, $params = array(),
- $full_uri = false
- ) {
- $uri = self::getRequestUri($path, $params, $full_uri);
- return $this->_makeIdempotentRequest(array($this->http, 'get'),
- $uri, $this->retryAttempts);
- }
-
- public function deleteData($path, $params = array())
- {
- $uri = self::getRequestUri($path, $params);
- return $this->_makeIdempotentRequest(array($this->http, 'delete'),
- $uri, $this->retryAttempts);
- }
-
- public function createData($path, $params = array())
- {
- $path = "$path.json";
- $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
- $response = $this->http->post(
- $path, $headers, self::buildQuery($params, '')
- );
- return $this->_processResponse($response);
- }
-
- public static function buildQuery($queryData, $numericPrefix = '') {
- $query = '';
-
- foreach ($queryData as $key => $value) {
-
- if (is_int($key)) {
- $key = $numericPrefix . $key;
- }
-
- if (is_array($value)) {
-
- foreach ($value as $value2) {
-
- if ($query !== '') {
- $query .= '&';
- }
-
- $query .= self::buildQuery(array($key => $value2), $numericPrefix);
- }
- } else {
-
- if ($query !== '') {
- $query .= '&';
- }
-
- $query .= $key . '=' . urlencode((string)$value);
- }
- }
- return $query;
- }
-
- private function _processResponse($response)
- {
- list($status, $headers, $body) = $response;
- if ($status === 204) {
- return true;
- }
- $decoded = json_decode($body);
- if ($decoded === null) {
- throw new Services_Twilio_RestException(
- $status,
- 'Could not decode response body as JSON. ' .
- 'This likely indicates a 500 server error'
- );
- }
- if (200 <= $status && $status < 300) {
- $this->last_response = $decoded;
- return $decoded;
- }
- throw new Services_Twilio_RestException(
- $status,
- isset($decoded->message) ? $decoded->message : '',
- isset($decoded->code) ? $decoded->code : null,
- isset($decoded->more_info) ? $decoded->more_info : null
- );
- }
- }
|