1 : <?php
2 :
3 : function Services_Twilio_autoload($className) {
4 11 : if (substr($className, 0, 15) != 'Services_Twilio') {
5 0 : return false;
6 : }
7 11 : $file = str_replace('_', '/', $className);
8 11 : $file = str_replace('Services/', '', $file);
9 11 : return include dirname(__FILE__) . "/$file.php";
10 : }
11 :
12 : spl_autoload_register('Services_Twilio_autoload');
13 :
14 : /**
15 : * Twilio API client interface.
16 : *
17 : * @category Services
18 : * @package Services_Twilio
19 : * @author Neuman Vong <neuman@twilio.com>
20 : * @license http://creativecommons.org/licenses/MIT/ MIT
21 : * @link http://pear.php.net/package/Services_Twilio
22 : */
23 : class Services_Twilio extends Services_Twilio_Resource
24 : {
25 : const USER_AGENT = 'twilio-php/3.3.2';
26 :
27 : protected $http;
28 : protected $version;
29 : protected $versions = array('2008-08-01', '2010-04-01');
30 :
31 : /**
32 : * Constructor.
33 : *
34 : * @param string $sid Account SID
35 : * @param string $token Account auth token
36 : * @param string $version API version
37 : * @param Services_Twilio_Http $_http A HTTP client
38 : */
39 : public function __construct(
40 : $sid,
41 : $token,
42 : $version = null,
43 : Services_Twilio_TinyHttp $_http = null
44 : ) {
45 27 : $this->version = in_array($version, $this->versions) ?
46 27 : $version : end($this->versions);
47 :
48 27 : if (null === $_http) {
49 1 : $_http = new Services_Twilio_TinyHttp(
50 1 : "https://api.twilio.com",
51 : array("curlopts" => array(
52 1 : CURLOPT_USERAGENT => self::USER_AGENT,
53 1 : CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
54 1 : CURLOPT_CAINFO => dirname(__FILE__) . "/twilio_ssl_certificate.crt",
55 1 : ))
56 1 : );
57 1 : }
58 27 : $_http->authenticate($sid, $token);
59 27 : $this->http = $_http;
60 27 : $this->accounts = new Services_Twilio_Rest_Accounts($this);
61 27 : $this->account = $this->accounts->get($sid);
62 27 : }
63 :
64 : /**
65 : * Get the api version used by the rest client
66 : *
67 : * @return string the API version in use
68 : */
69 : public function getVersion() {
70 1 : return $this->version;
71 : }
72 :
73 : /**
74 : * GET the resource at the specified path.
75 : *
76 : * @param string $path Path to the resource
77 : * @param array $params Query string parameters
78 : *
79 : * @return object The object representation of the resource
80 : */
81 : public function retrieveData($path, array $params = array())
82 : {
83 17 : $path = "/$this->version/$path.json";
84 17 : return empty($params)
85 17 : ? $this->_processResponse($this->http->get($path))
86 16 : : $this->_processResponse(
87 9 : $this->http->get("$path?" . http_build_query($params, '', '&'))
88 16 : );
89 : }
90 :
91 : /**
92 : * DELETE the resource at the specified path.
93 : *
94 : * @param string $path Path to the resource
95 : * @param array $params Query string parameters
96 : *
97 : * @return object The object representation of the resource
98 : */
99 : public function deleteData($path, array $params = array())
100 : {
101 1 : $path = "/$this->version/$path.json";
102 1 : return empty($params)
103 1 : ? $this->_processResponse($this->http->delete($path))
104 1 : : $this->_processResponse(
105 0 : $this->http->delete("$path?" . http_build_query($params, '', '&'))
106 1 : );
107 : }
108 :
109 : /**
110 : * POST to the resource at the specified path.
111 : *
112 : * @param string $path Path to the resource
113 : * @param array $params Query string parameters
114 : *
115 : * @return object The object representation of the resource
116 : */
117 : public function createData($path, array $params = array())
118 : {
119 11 : $path = "/$this->version/$path.json";
120 11 : $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
121 11 : return empty($params)
122 11 : ? $this->_processResponse($this->http->post($path, $headers))
123 11 : : $this->_processResponse(
124 11 : $this->http->post(
125 11 : $path,
126 11 : $headers,
127 11 : http_build_query($params, '', '&')
128 11 : )
129 11 : );
130 : }
131 :
132 : /**
133 : * Convert the JSON encoded resource into a PHP object.
134 : *
135 : * @param array $response 3-tuple containing status, headers, and body
136 : *
137 : * @return object PHP object decoded from JSON
138 : */
139 : private function _processResponse($response)
140 : {
141 25 : list($status, $headers, $body) = $response;
142 25 : if ($status == 204) {
143 1 : return TRUE;
144 : }
145 24 : if (empty($headers['Content-Type'])) {
146 1 : throw new DomainException('Response header is missing Content-Type');
147 : }
148 23 : return $this->_processJsonResponse($status, $headers, $body);
149 : }
150 :
151 : private function _processJsonResponse($status, $headers, $body) {
152 23 : $decoded = json_decode($body);
153 23 : if (200 <= $status && $status < 300) {
154 22 : return $decoded;
155 : }
156 2 : throw new Services_Twilio_RestException(
157 2 : (int)$decoded->status,
158 2 : $decoded->message,
159 2 : isset($decoded->code) ? $decoded->code : null,
160 2 : isset($decoded->more_info) ? $decoded->more_info : null
161 2 : );
162 : }
163 :
164 : }
|