1 : <?php
2 : /**
3 : * Based on TinyHttp from https://gist.github.com/618157.
4 : * Copyright 2011, Neuman Vong. BSD License.
5 : */
6 :
7 1 : class Services_Twilio_TinyHttpException extends ErrorException {}
8 :
9 : class Services_Twilio_TinyHttp {
10 : var $user, $pass, $scheme, $host, $port, $debug, $curlopts;
11 :
12 : public function __construct($uri = '', $kwargs = array()) {
13 27 : foreach (parse_url($uri) as $name => $value) $this->$name = $value;
14 27 : $this->debug = isset($kwargs['debug']) ? !!$kwargs['debug'] : NULL;
15 27 : $this->curlopts = isset($kwargs['curlopts']) ? $kwargs['curlopts'] : array();
16 27 : }
17 :
18 : public function __call($name, $args) {
19 0 : list($res, $req_headers, $req_body) = $args + array(0, array(), '');
20 :
21 0 : $opts = $this->curlopts + array(
22 0 : CURLOPT_URL => "$this->scheme://$this->host$res",
23 0 : CURLOPT_HEADER => TRUE,
24 0 : CURLOPT_RETURNTRANSFER => TRUE,
25 0 : CURLOPT_INFILESIZE => -1,
26 0 : CURLOPT_POSTFIELDS => NULL,
27 0 : CURLOPT_TIMEOUT => 60,
28 0 : );
29 :
30 0 : foreach ($req_headers as $k => $v) $opts[CURLOPT_HTTPHEADER][] = "$k: $v";
31 0 : if ($this->port) $opts[CURLOPT_PORT] = $this->port;
32 0 : if ($this->debug) $opts[CURLINFO_HEADER_OUT] = TRUE;
33 0 : if ($this->user && $this->pass) $opts[CURLOPT_USERPWD] = "$this->user:$this->pass";
34 : switch ($name) {
35 0 : case 'get':
36 0 : $opts[CURLOPT_HTTPGET] = TRUE;
37 0 : break;
38 0 : case 'post':
39 0 : $opts[CURLOPT_POST] = TRUE;
40 0 : $opts[CURLOPT_POSTFIELDS] = $req_body;
41 0 : break;
42 0 : case 'put':
43 0 : $opts[CURLOPT_PUT] = TRUE;
44 0 : if (strlen($req_body)) {
45 0 : if ($buf = fopen('php://memory', 'w+')) {
46 0 : fwrite($buf, $req_body);
47 0 : fseek($buf, 0);
48 0 : $opts[CURLOPT_INFILE] = $buf;
49 0 : $opts[CURLOPT_INFILESIZE] = strlen($req_body);
50 0 : } else throw new Services_Twilio_TinyHttpException('unable to open temporary file');
51 0 : }
52 0 : break;
53 0 : case 'head':
54 0 : $opts[CURLOPT_NOBODY] = TRUE;
55 0 : break;
56 0 : default:
57 0 : $opts[CURLOPT_CUSTOMREQUEST] = strtoupper($name);
58 0 : break;
59 0 : }
60 : try {
61 0 : if ($curl = curl_init()) {
62 0 : if (curl_setopt_array($curl, $opts)) {
63 0 : if ($response = curl_exec($curl)) {
64 0 : $parts = explode("\r\n\r\n", $response, 3);
65 0 : list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue')
66 0 : ? array($parts[1], $parts[2])
67 0 : : array($parts[0], $parts[1]);
68 0 : $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
69 0 : if ($this->debug) {
70 0 : error_log(
71 0 : curl_getinfo($curl, CURLINFO_HEADER_OUT) .
72 : $req_body
73 0 : );
74 0 : }
75 0 : $header_lines = explode("\r\n", $head);
76 0 : array_shift($header_lines);
77 0 : foreach ($header_lines as $line) {
78 0 : list($key, $value) = explode(":", $line, 2);
79 0 : $headers[$key] = trim($value);
80 0 : }
81 0 : curl_close($curl);
82 0 : if (isset($buf) && is_resource($buf)) fclose($buf);
83 0 : return array($status, $headers, $body);
84 0 : } else throw new Services_Twilio_TinyHttpException(curl_error($curl));
85 0 : } else throw new Services_Twilio_TinyHttpException(curl_error($curl));
86 0 : } else throw new Services_Twilio_TinyHttpException('unable to initialize cURL');
87 0 : } catch (ErrorException $e) {
88 0 : if (is_resource($curl)) curl_close($curl);
89 0 : if (isset($buf) && is_resource($buf)) fclose($buf);
90 0 : throw $e;
91 : }
92 : }
93 :
94 : public function authenticate($user, $pass) {
95 27 : $this->user = $user;
96 27 : $this->pass = $pass;
97 27 : }
98 : }
|