version 3.0
This commit is contained in:
46
lib/Chunked.php
Executable file
46
lib/Chunked.php
Executable file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
if (!function_exists('http_chunked_decode')) {
|
||||
/**
|
||||
* dechunk an http 'transfer-encoding: chunked' message
|
||||
*
|
||||
* @param string $chunk the encoded message
|
||||
* @return string the decoded message. If $chunk wasn't encoded properly it will be returned unmodified.
|
||||
*/
|
||||
function http_chunked_decode($chunk) {
|
||||
$pos = 0;
|
||||
$len = strlen($chunk);
|
||||
$dechunk = null;
|
||||
|
||||
while(($pos < $len)
|
||||
&& ($chunkLenHex = substr($chunk,$pos, ($newlineAt = strpos($chunk,"\n",$pos+1))-$pos)))
|
||||
{
|
||||
if (! is_hex($chunkLenHex)) {
|
||||
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
|
||||
return $chunk;
|
||||
}
|
||||
|
||||
$pos = $newlineAt + 1;
|
||||
$chunkLen = hexdec(rtrim($chunkLenHex,"\r\n"));
|
||||
$dechunk .= substr($chunk, $pos, $chunkLen);
|
||||
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
|
||||
}
|
||||
return $dechunk;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* determine if a string can represent a number in hexadecimal
|
||||
*
|
||||
* @param string $hex
|
||||
* @return boolean true if the string is a hex, otherwise false
|
||||
*/
|
||||
function is_hex($hex) {
|
||||
// regex is for weenies
|
||||
$hex = strtolower(trim(ltrim($hex,"0")));
|
||||
if (empty($hex)) { $hex = 0; };
|
||||
$dec = hexdec($hex);
|
||||
return ($hex == dechex($dec));
|
||||
}
|
||||
|
||||
?>
|
||||
105
lib/HttpClient.php
Normal file
105
lib/HttpClient.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
require_once(dirname(__FILE__) . "/Chunked.php");
|
||||
require_once(dirname(__FILE__) . "/HttpResponse.php");
|
||||
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
||||
|
||||
class HttpClient {
|
||||
|
||||
const USER_AGENT = "mashape-php/3.0";
|
||||
|
||||
public static function get($url, $headers = array()) {
|
||||
return HttpClient::request(HttpMethod::GET, $url, NULL, $headers);
|
||||
}
|
||||
|
||||
public static function post($url, $body = NULL, $headers = array()) {
|
||||
return HttpClient::request(HttpMethod::POST, $url, $body, $headers);
|
||||
}
|
||||
|
||||
private static function request($httpMethod, $url, $body = NULL, $headers = array()) {
|
||||
|
||||
$lowercaseHeaders = array();
|
||||
foreach ($headers as $key => $val) {
|
||||
$lowercaseHeaders[strtolower($key)] = $val;
|
||||
}
|
||||
|
||||
$lowercaseHeaders["user-agent"] = USER_AGENT;
|
||||
|
||||
$ch = curl_init();
|
||||
if ($httpMethod != HttpMethod::GET) {
|
||||
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
||||
//TODO: Remove
|
||||
/*
|
||||
if (is_array($body)) {
|
||||
$parameters = "";
|
||||
foreach($body as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
throw new Exception("Nested arrays are not supported");
|
||||
}
|
||||
|
||||
$parameters .= $key . "=";
|
||||
if (substr($value, 0, 1) == "@") {
|
||||
// It's a path
|
||||
$parameters .= $value;
|
||||
} else {
|
||||
$parameters .= rawurlencode($value);
|
||||
}
|
||||
|
||||
$parameters .= "&";
|
||||
}
|
||||
|
||||
if (strlen($parameters) > 1) {
|
||||
$parameters = substr($parameters, 0, strlen($parameters) - 1);
|
||||
}
|
||||
|
||||
$body = $parameters;
|
||||
var_dump($body);
|
||||
}
|
||||
*/
|
||||
curl_setopt ($ch, CURLOPT_POSTFIELDS, $body);
|
||||
}
|
||||
|
||||
curl_setopt ($ch, CURLOPT_URL , HttpClient::encodeUrl($url));
|
||||
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt ($ch, CURLOPT_MAXREDIRS, 10);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
|
||||
curl_setopt ($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders);
|
||||
curl_setopt ($ch, CURLOPT_HEADER, true);
|
||||
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
if (curl_error($ch)) {
|
||||
throw new Exception(curl_error($ch));
|
||||
}
|
||||
|
||||
// Split the full response in its headers and body
|
||||
$curl_info = curl_getinfo($ch);
|
||||
$header_size = $curl_info["header_size"];
|
||||
$header = substr($response, 0, $header_size);
|
||||
$body = substr($response, $header_size);
|
||||
$httpCode = $curl_info["http_code"];
|
||||
|
||||
return new HttpResponse($httpCode, $body, $header);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static function encodeUrl($url) {
|
||||
$parsedUrl = parse_url($url);
|
||||
parse_str( $parsedUrl['query'], $query ); // generating an array by reference (yes, kinda weird)
|
||||
|
||||
$result = $parsedUrl["scheme"] . "://" . $parsedUrl["host"] . (($parsedUrl["port"] != NULL ? ":" . $parsedUrl["port"] : "")) . $parsedUrl["path"] . "?";
|
||||
|
||||
if ($query != null) {
|
||||
foreach($query as $key => $val) {
|
||||
$result .= $key . "=" . rawurlencode($val) . "&";
|
||||
}
|
||||
$result = substr($result, 0, strlen($result) - 1);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
36
lib/HttpMethod.php
Executable file
36
lib/HttpMethod.php
Executable file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Mashape PHP Client library.
|
||||
*
|
||||
* Copyright (C) 2011 Mashape, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
* The author of this software is Mashape, Inc.
|
||||
* For any question or feedback please contact us at: support@mashape.com
|
||||
*
|
||||
*/
|
||||
|
||||
class HttpMethod
|
||||
{
|
||||
const DELETE = "DELETE";
|
||||
const GET = "GET";
|
||||
const POST = "POST";
|
||||
const PUT = "PUT";
|
||||
const PATCH = "PATCH";
|
||||
}
|
||||
|
||||
?>
|
||||
48
lib/HttpResponse.php
Normal file
48
lib/HttpResponse.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
class HttpResponse {
|
||||
|
||||
private $code;
|
||||
private $raw_body;
|
||||
private $body;
|
||||
private $headers;
|
||||
|
||||
function __construct($code, $raw_body, $headers) {
|
||||
$this->code = $code;
|
||||
$this->headers = $this->get_headers_from_curl_response($headers);
|
||||
$this->raw_body = $raw_body;
|
||||
$this->body = $raw_body;
|
||||
$json = json_decode($raw_body);
|
||||
if (json_last_error() == JSON_ERROR_NONE) {
|
||||
$this->body = $json;
|
||||
}
|
||||
}
|
||||
|
||||
public function __get($property) {
|
||||
if (property_exists($this, $property)) {
|
||||
return $this->$property;
|
||||
}
|
||||
}
|
||||
|
||||
public function __set($property, $value) {
|
||||
if (property_exists($this, $property)) {
|
||||
$this->$property = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function get_headers_from_curl_response($headers)
|
||||
{
|
||||
foreach (explode("\r\n", $headers) as $i => $line) {
|
||||
if ($i !== 0) {
|
||||
list ($key, $value) = explode(': ', $line);
|
||||
if (!empty($key) && substr($key, 0, 4) != "HTTP") $result[$key] = $value;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user