1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-15 03:12:41 +01:00
phorge-phorge/externals/twilio-php/Services/Twilio/HttpStream.php
Bob Trahan e96c363eef Add SMS support
Summary:
Provides a working SMS implementation with support for Twilio.

This version doesn't really retry if we get any gruff at all. Future versions should retry.

Test Plan: used bin/sms to send messages and look at them.

Reviewers: chad, epriestley

Reviewed By: epriestley

Subscribers: aurelijus, epriestley, Korvin

Maniphest Tasks: T920

Differential Revision: https://secure.phabricator.com/D8930
2014-05-09 12:47:21 -07:00

94 lines
2.9 KiB
PHP
Executable file

<?php
/**
* HTTP Stream version of the TinyHttp Client used to connect to Twilio
* services.
*/
class Services_Twilio_HttpStreamException extends ErrorException {}
class Services_Twilio_HttpStream {
private $auth_header = null;
private $uri = null;
private $debug = false;
private static $default_options = array(
"http" => array(
"headers" => "",
"timeout" => 60,
"follow_location" => true,
"ignore_errors" => true,
),
"ssl" => array(),
);
private $options = array();
public function __construct($uri = '', $kwargs = array()) {
$this->uri = $uri;
if (isset($kwargs['debug'])) {
$this->debug = true;
}
if (isset($kwargs['http_options'])) {
$this->options = $kwargs['http_options'] + self::$default_options;
} else {
$this->options = self::$default_options;
}
}
public function __call($name, $args) {
list($res, $req_headers, $req_body) = $args + array(0, array(), '');
$request_options = $this->options;
$url = $this->uri . $res;
if (isset($req_body) && strlen($req_body) > 0) {
$request_options['http']['content'] = $req_body;
}
foreach($req_headers as $key => $value) {
$request_options['http']['header'] .= sprintf("%s: %s\r\n", $key, $value);
}
if (isset($this->auth_header)) {
$request_options['http']['header'] .= $this->auth_header;
}
$request_options['http']['method'] = strtoupper($name);
$request_options['http']['ignore_errors'] = true;
if ($this->debug) {
error_log(var_export($request_options, true));
}
$ctx = stream_context_create($request_options);
$result = file_get_contents($url, false, $ctx);
if (false === $result) {
throw new Services_Twilio_HttpStreamException(
"Unable to connect to service");
}
$status_header = array_shift($http_response_header);
if (1 !== preg_match('#HTTP/\d+\.\d+ (\d+)#', $status_header, $matches)) {
throw new Services_Twilio_HttpStreamException(
"Unable to detect the status code in the HTTP result.");
}
$status_code = intval($matches[1]);
$response_headers = array();
foreach($http_response_header as $header) {
list($key, $val) = explode(":", $header);
$response_headers[trim($key)] = trim($val);
}
return array($status_code, $response_headers, $result);
}
public function authenticate($user, $pass) {
if (isset($user) && isset($pass)) {
$this->auth_header = sprintf("Authorization: Basic %s",
base64_encode(sprintf("%s:%s", $user, $pass)));
} else {
$this->auth_header = null;
}
}
}