A very small HTTP client with Rack/WSGI style interfaces.
$sid = 'ACxxxxxxxxxxxx';
$token = '12345678';
$http = new TinyHttp("https://$sid:$token@api.twilio.com");
Tiny takes a 3-tuple comprising the request path, headers, and body. Only the request path is required, the headers and body are optional.
$response = $http->get("/2010-04-01/Accounts/$sid.json");
The response is a 3-tuple of the response code, headers, and body. Just like a Rack or WSGI application.
list($status, $headers, $body) = $response;
The status is a number, headers are an array, and the body is a string.
if ($status == 200 && $headers['Content-Type'] == 'application/json') {
$account = json_decode($body);
var_dump($account->friendly_name);
}
$http->post(
"/2010-04-01/Accounts/$sid.json",
array('Content-Type' => 'application/x-www-form-urlencoded'),
http_build_query(array('friendly_name' => "Benny's World of Liquor"))
)
I found a bug when parsing XML with CDATA and multiple line-breaks. In that case, your "explode" will push the XML beyond the line breaks into inaccessible indices in $parts. "explode" is used naively here. So, we want to split the string instead of exploding it. I was able to fix this by replacing lines 72-75 in your code with the following lines:
$head = null;
$body = $response;
$sep = (strpos($response, "\r\n\r\n") === false ? "\n\n" : "\r\n\r\n");
while ((empty($head) || strcasecmp($head, 'HTTP/1.1 100 Continue') == 0) && ($pos = strpos($body, $sep)) !== false) {
$head = substr($body, 0, $pos);
$body = substr($body, $pos+strlen($sep));
}