[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Based on TinyHttp from https://gist.github.com/618157. 4 * Copyright 2011, Neuman Vong. BSD License. 5 */ 6 7 class Services_Twilio_TinyHttpException extends ErrorException {} 8 9 /** 10 * An HTTP client that makes requests 11 * 12 * :param string $uri: The base uri to use for requests 13 * :param array $kwargs: An array of additional arguments to pass to the 14 * library. Accepted arguments are: 15 * 16 * - **debug** - Print the HTTP request before making it to Twilio 17 * - **curlopts** - An array of keys and values that are passed to 18 * ``curl_setopt_array``. 19 * 20 * Here's an example. This is the default HTTP client used by the library. 21 * 22 * .. code-block:: php 23 * 24 * $_http = new Services_Twilio_TinyHttp( 25 * "https://api.twilio.com", 26 * array("curlopts" => array( 27 * CURLOPT_USERAGENT => self::USER_AGENT, 28 * CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'), 29 * CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem', 30 * )) 31 * ); 32 */ 33 class Services_Twilio_TinyHttp { 34 var $user, $pass, $scheme, $host, $port, $debug, $curlopts; 35 36 public function __construct($uri = '', $kwargs = array()) { 37 foreach (parse_url($uri) as $name => $value) $this->$name = $value; 38 $this->debug = isset($kwargs['debug']) ? !!$kwargs['debug'] : NULL; 39 $this->curlopts = isset($kwargs['curlopts']) ? $kwargs['curlopts'] : array(); 40 } 41 42 public function __call($name, $args) { 43 list($res, $req_headers, $req_body) = $args + array(0, array(), ''); 44 45 $opts = $this->curlopts + array( 46 CURLOPT_URL => "$this->scheme://$this->host$res", 47 CURLOPT_HEADER => TRUE, 48 CURLOPT_RETURNTRANSFER => TRUE, 49 CURLOPT_INFILESIZE => -1, 50 CURLOPT_POSTFIELDS => NULL, 51 CURLOPT_TIMEOUT => 60, 52 ); 53 54 foreach ($req_headers as $k => $v) $opts[CURLOPT_HTTPHEADER][] = "$k: $v"; 55 if ($this->port) $opts[CURLOPT_PORT] = $this->port; 56 if ($this->debug) $opts[CURLINFO_HEADER_OUT] = TRUE; 57 if ($this->user && $this->pass) $opts[CURLOPT_USERPWD] = "$this->user:$this->pass"; 58 switch ($name) { 59 case 'get': 60 $opts[CURLOPT_HTTPGET] = TRUE; 61 break; 62 case 'post': 63 $opts[CURLOPT_POST] = TRUE; 64 $opts[CURLOPT_POSTFIELDS] = $req_body; 65 break; 66 case 'put': 67 $opts[CURLOPT_PUT] = TRUE; 68 if (strlen($req_body)) { 69 if ($buf = fopen('php://memory', 'w+')) { 70 fwrite($buf, $req_body); 71 fseek($buf, 0); 72 $opts[CURLOPT_INFILE] = $buf; 73 $opts[CURLOPT_INFILESIZE] = strlen($req_body); 74 } else throw new Services_Twilio_TinyHttpException('unable to open temporary file'); 75 } 76 break; 77 case 'head': 78 $opts[CURLOPT_NOBODY] = TRUE; 79 break; 80 default: 81 $opts[CURLOPT_CUSTOMREQUEST] = strtoupper($name); 82 break; 83 } 84 try { 85 if ($curl = curl_init()) { 86 if (curl_setopt_array($curl, $opts)) { 87 if ($response = curl_exec($curl)) { 88 $parts = explode("\r\n\r\n", $response, 3); 89 list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue') 90 ? array($parts[1], $parts[2]) 91 : array($parts[0], $parts[1]); 92 $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 93 if ($this->debug) { 94 error_log( 95 curl_getinfo($curl, CURLINFO_HEADER_OUT) . 96 $req_body 97 ); 98 } 99 $header_lines = explode("\r\n", $head); 100 array_shift($header_lines); 101 foreach ($header_lines as $line) { 102 list($key, $value) = explode(":", $line, 2); 103 $headers[$key] = trim($value); 104 } 105 curl_close($curl); 106 if (isset($buf) && is_resource($buf)) { 107 fclose($buf); 108 } 109 return array($status, $headers, $body); 110 } else { 111 throw new Services_Twilio_TinyHttpException(curl_error($curl)); 112 } 113 } else throw new Services_Twilio_TinyHttpException(curl_error($curl)); 114 } else throw new Services_Twilio_TinyHttpException('unable to initialize cURL'); 115 } catch (ErrorException $e) { 116 if (is_resource($curl)) curl_close($curl); 117 if (isset($buf) && is_resource($buf)) fclose($buf); 118 throw $e; 119 } 120 } 121 122 public function authenticate($user, $pass) { 123 $this->user = $user; 124 $this->pass = $pass; 125 } 126 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |