Merge pull request #21 from andreyknupp/master
Improving the library, indenting the code.
This commit is contained in:
@@ -1,9 +1,15 @@
|
|||||||
<?php namespace Unirest;
|
<?php
|
||||||
class HttpMethod
|
|
||||||
{
|
namespace Unirest;
|
||||||
|
|
||||||
|
interface HttpMethod
|
||||||
|
{
|
||||||
|
|
||||||
const DELETE = "DELETE";
|
const DELETE = "DELETE";
|
||||||
const GET = "GET";
|
const GET = "GET";
|
||||||
const POST = "POST";
|
const POST = "POST";
|
||||||
const PUT = "PUT";
|
const PUT = "PUT";
|
||||||
const PATCH = "PATCH";
|
const PATCH = "PATCH";
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,38 +1,37 @@
|
|||||||
<?php namespace Unirest;
|
<?php
|
||||||
class HttpResponse
|
|
||||||
{
|
namespace Unirest;
|
||||||
|
|
||||||
|
class HttpResponse
|
||||||
|
{
|
||||||
|
|
||||||
private $code;
|
private $code;
|
||||||
private $raw_body;
|
private $raw_body;
|
||||||
private $body;
|
private $body;
|
||||||
private $headers;
|
private $headers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HttpResponse constructor
|
* @param int $code response code of the cURL request
|
||||||
* @param int $code Response code of the cURL request
|
* @param string $raw_body the raw body of the cURL response
|
||||||
* @param string $raw_body The raw body of the cURL response
|
* @param string $headers raw header string from cURL response
|
||||||
* @param string $headers Raw header string from cURL response
|
|
||||||
*/
|
*/
|
||||||
function __construct($code, $raw_body, $headers)
|
public function __construct($code, $raw_body, $headers)
|
||||||
{
|
{
|
||||||
$this->code = $code;
|
$this->code = $code;
|
||||||
$this->headers = $this->get_headers_from_curl_response($headers);
|
$this->headers = $this->get_headers_from_curl_response($headers);
|
||||||
$this->raw_body = $raw_body;
|
$this->raw_body = $raw_body;
|
||||||
$this->body = $raw_body;
|
$this->body = $raw_body;
|
||||||
$json = json_decode($raw_body);
|
$json = json_decode($raw_body);
|
||||||
|
|
||||||
if (json_last_error() == JSON_ERROR_NONE) {
|
if (json_last_error() == JSON_ERROR_NONE) {
|
||||||
$this->body = $json;
|
$this->body = $json;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a property of the response if it exists
|
* Return a property of the response if it exists.
|
||||||
* Possibilities include:
|
* Possibilities include: code, raw_body, headers, body (if the response is json-decodable)
|
||||||
* - code
|
* @return mixed
|
||||||
* - raw_body
|
|
||||||
* - body (if the response is json-decodable)
|
|
||||||
* - headers
|
|
||||||
* @param [type] $property [description]
|
|
||||||
* @return [type] [description]
|
|
||||||
*/
|
*/
|
||||||
public function __get($property)
|
public function __get($property)
|
||||||
{
|
{
|
||||||
@@ -43,8 +42,8 @@ class HttpResponse
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the properties of this object
|
* Set the properties of this object
|
||||||
* @param string $property The property name
|
* @param string $property the property name
|
||||||
* @param mixed $value The property value
|
* @param mixed $value the property value
|
||||||
*/
|
*/
|
||||||
public function __set($property, $value)
|
public function __set($property, $value)
|
||||||
{
|
{
|
||||||
@@ -58,7 +57,7 @@ class HttpResponse
|
|||||||
* Retrieve the cURL response headers from the
|
* Retrieve the cURL response headers from the
|
||||||
* header string and convert it into an array
|
* header string and convert it into an array
|
||||||
* @param string $headers header string from cURL response
|
* @param string $headers header string from cURL response
|
||||||
* @return array headers in array form
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function get_headers_from_curl_response($headers)
|
private function get_headers_from_curl_response($headers)
|
||||||
{
|
{
|
||||||
@@ -74,4 +73,5 @@ class HttpResponse
|
|||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
@@ -1,15 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Unirest\HttpMethod;
|
use Unirest\HttpMethod;
|
||||||
use Unirest\HttpResponse;
|
use Unirest\HttpResponse;
|
||||||
|
|
||||||
|
class Unirest
|
||||||
|
{
|
||||||
|
|
||||||
class Unirest
|
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Send a GET request to a URL
|
* Send a GET request to a URL
|
||||||
* @param string $url URL to send the GET request to
|
* @param string $url URL to send the GET request to
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
* @return string|stdObj response string or stdObj if response is json-decodable
|
||||||
*/
|
*/
|
||||||
public static function get($url, $headers = array())
|
public static function get($url, $headers = array())
|
||||||
{
|
{
|
||||||
@@ -19,9 +20,9 @@ class Unirest
|
|||||||
/**
|
/**
|
||||||
* Send POST request to a URL
|
* Send POST request to a URL
|
||||||
* @param string $url URL to send the POST request to
|
* @param string $url URL to send the POST request to
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @param mixed $body POST body data
|
* @param mixed $body POST body data
|
||||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
* @return string|stdObj response string or stdObj if response is json-decodable
|
||||||
*/
|
*/
|
||||||
public static function post($url, $headers = array(), $body = NULL)
|
public static function post($url, $headers = array(), $body = NULL)
|
||||||
{
|
{
|
||||||
@@ -31,8 +32,8 @@ class Unirest
|
|||||||
/**
|
/**
|
||||||
* Send DELETE request to a URL
|
* Send DELETE request to a URL
|
||||||
* @param string $url URL to send the DELETE request to
|
* @param string $url URL to send the DELETE request to
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
* @return string|stdObj response string or stdObj if response is json-decodable
|
||||||
*/
|
*/
|
||||||
public static function delete($url, $headers = array())
|
public static function delete($url, $headers = array())
|
||||||
{
|
{
|
||||||
@@ -42,9 +43,9 @@ class Unirest
|
|||||||
/**
|
/**
|
||||||
* Send PUT request to a URL
|
* Send PUT request to a URL
|
||||||
* @param string $url URL to send the PUT request to
|
* @param string $url URL to send the PUT request to
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @param mixed $body PUT body data
|
* @param mixed $body PUT body data
|
||||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
* @return string|stdObj response string or stdObj if response is json-decodable
|
||||||
*/
|
*/
|
||||||
public static function put($url, $headers = array(), $body = NULL)
|
public static function put($url, $headers = array(), $body = NULL)
|
||||||
{
|
{
|
||||||
@@ -54,9 +55,9 @@ class Unirest
|
|||||||
/**
|
/**
|
||||||
* Send PATCH request to a URL
|
* Send PATCH request to a URL
|
||||||
* @param string $url URL to send the PATCH request to
|
* @param string $url URL to send the PATCH request to
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @param mixed $body PATCH body data
|
* @param mixed $body PATCH body data
|
||||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
* @return string|stdObj response string or stdObj if response is json-decodable
|
||||||
*/
|
*/
|
||||||
public static function patch($url, $headers = array(), $body = NULL)
|
public static function patch($url, $headers = array(), $body = NULL)
|
||||||
{
|
{
|
||||||
@@ -65,19 +66,20 @@ class Unirest
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a cURL request
|
* Send a cURL request
|
||||||
* @param string $httpMethod HTTP Method to use (based off \Unirest\HttpMethod constants)
|
* @param string $httpMethod HTTP method to use (based off \Unirest\HttpMethod constants)
|
||||||
* @param string $url URL to send the request to
|
* @param string $url URL to send the request to
|
||||||
* @param mixed $body Request body
|
* @param mixed $body request body
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @throws Exception If a cURL error occurs
|
* @throws Exception if a cURL error occurs
|
||||||
* @return \Unireset\HttpResponse \Unirest\HttpResponse object
|
* @return HttpResponse
|
||||||
*/
|
*/
|
||||||
private static function request($httpMethod, $url, $body = NULL, $headers = array())
|
private static function request($httpMethod, $url, $body = NULL, $headers = array())
|
||||||
{
|
{
|
||||||
$lowercaseHeaders = array();
|
$lowercaseHeaders = array();
|
||||||
foreach ($headers as $key => $val) {
|
foreach ($headers as $key => $val) {
|
||||||
$key = trim(strtolower($key));
|
$key = trim(strtolower($key));
|
||||||
if ($key == "user-agent" || $key == "expect") continue;
|
if ($key == "user-agent" || $key == "expect")
|
||||||
|
continue;
|
||||||
$lowercaseHeaders[] = $key . ": " . $val;
|
$lowercaseHeaders[] = $key . ": " . $val;
|
||||||
}
|
}
|
||||||
$lowercaseHeaders[] = "user-agent: unirest-php/1.0";
|
$lowercaseHeaders[] = "user-agent: unirest-php/1.0";
|
||||||
@@ -85,17 +87,17 @@ class Unirest
|
|||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
if ($httpMethod != HttpMethod::GET) {
|
if ($httpMethod != HttpMethod::GET) {
|
||||||
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
||||||
curl_setopt ($ch, CURLOPT_POSTFIELDS, $body);
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_setopt ($ch, CURLOPT_URL , Unirest::encodeUrl($url));
|
curl_setopt($ch, CURLOPT_URL, Unirest::encodeUrl($url));
|
||||||
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||||
curl_setopt ($ch, CURLOPT_MAXREDIRS, 10);
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
|
||||||
curl_setopt ($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders);
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders);
|
||||||
curl_setopt ($ch, CURLOPT_HEADER, true);
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
||||||
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
$error = curl_error($ch);
|
$error = curl_error($ch);
|
||||||
@@ -103,7 +105,6 @@ class Unirest
|
|||||||
throw new \Exception($error);
|
throw new \Exception($error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split the full response in its headers and body
|
|
||||||
$curl_info = curl_getinfo($ch);
|
$curl_info = curl_getinfo($ch);
|
||||||
$header_size = $curl_info["header_size"];
|
$header_size = $curl_info["header_size"];
|
||||||
$header = substr($response, 0, $header_size);
|
$header = substr($response, 0, $header_size);
|
||||||
@@ -116,78 +117,70 @@ class Unirest
|
|||||||
/**
|
/**
|
||||||
* Ensure that a URL is encoded and safe to use with cURL
|
* Ensure that a URL is encoded and safe to use with cURL
|
||||||
* @param string $url URL to encode
|
* @param string $url URL to encode
|
||||||
* @return string Encoded URL
|
* @return string
|
||||||
*/
|
*/
|
||||||
private static function encodeUrl($url)
|
private static function encodeUrl($url)
|
||||||
{
|
{
|
||||||
// Parse URL into pieces
|
|
||||||
$url_parsed = parse_url($url);
|
$url_parsed = parse_url($url);
|
||||||
|
|
||||||
// Build the basics bypassing notices
|
|
||||||
$scheme = $url_parsed['scheme'] . '://';
|
$scheme = $url_parsed['scheme'] . '://';
|
||||||
$host = $url_parsed['host'];
|
$host = $url_parsed['host'];
|
||||||
$port = (isset($url_parsed['port']) ? $url_parsed['port'] : null );
|
$port = (isset($url_parsed['port']) ? $url_parsed['port'] : null);
|
||||||
$path = (isset($url_parsed['path']) ? $url_parsed['path'] : null );
|
$path = (isset($url_parsed['path']) ? $url_parsed['path'] : null);
|
||||||
$query = (isset($url_parsed['query']) ? $url_parsed['query'] : null );
|
$query = (isset($url_parsed['query']) ? $url_parsed['query'] : null);
|
||||||
|
|
||||||
// Do we need to encode anything?
|
|
||||||
if ($query != null) {
|
if ($query != null) {
|
||||||
// Break up the query into an array
|
|
||||||
parse_str($url_parsed['query'], $query_parsed);
|
parse_str($url_parsed['query'], $query_parsed);
|
||||||
// Encode and build query based on RFC 1738
|
$query = '?' . http_build_query($query_parsed);
|
||||||
$query = '?'.http_build_query($query_parsed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle port seperator
|
|
||||||
if ($port && $port[0] != ":")
|
if ($port && $port[0] != ":")
|
||||||
$port = ":" . $port;
|
$port = ":" . $port;
|
||||||
|
|
||||||
// Return the completed URL
|
|
||||||
$result = $scheme . $host . $port . $path . $query;
|
$result = $scheme . $host . $port . $path . $query;
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!function_exists('http_chunked_decode')) {
|
if (!function_exists('http_chunked_decode')) {
|
||||||
/**
|
/**
|
||||||
* dechunk an http 'transfer-encoding: chunked' message
|
* Dechunk an http 'transfer-encoding: chunked' message
|
||||||
*
|
|
||||||
* @param string $chunk the encoded message
|
* @param string $chunk the encoded message
|
||||||
* @return string the decoded message. If $chunk wasn't encoded properly it will be returned unmodified.
|
* @return string the decoded message
|
||||||
*/
|
*/
|
||||||
function http_chunked_decode($chunk) {
|
function http_chunked_decode($chunk)
|
||||||
|
{
|
||||||
$pos = 0;
|
$pos = 0;
|
||||||
$len = strlen($chunk);
|
$len = strlen($chunk);
|
||||||
$dechunk = null;
|
$dechunk = null;
|
||||||
|
|
||||||
while(($pos < $len)
|
while (($pos < $len)
|
||||||
&& ($chunkLenHex = substr($chunk,$pos, ($newlineAt = strpos($chunk,"\n",$pos+1))-$pos))
|
&& ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) {
|
||||||
) {
|
|
||||||
if (!is_hex($chunkLenHex)) {
|
if (!is_hex($chunkLenHex)) {
|
||||||
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
|
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
|
||||||
return $chunk;
|
return $chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
$pos = $newlineAt + 1;
|
$pos = $newlineAt + 1;
|
||||||
$chunkLen = hexdec(rtrim($chunkLenHex,"\r\n"));
|
$chunkLen = hexdec(rtrim($chunkLenHex, "\r\n"));
|
||||||
$dechunk .= substr($chunk, $pos, $chunkLen);
|
$dechunk .= substr($chunk, $pos, $chunkLen);
|
||||||
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
|
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dechunk;
|
return $dechunk;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* determine if a string can represent a number in hexadecimal
|
* determine if a string can represent a number in hexadecimal
|
||||||
*
|
* @link http://uk1.php.net/ctype_xdigit
|
||||||
* @param string $hex
|
* @param string $hex
|
||||||
* @return boolean true if the string is a hex, otherwise false
|
* @return boolean true if the string is a hex, otherwise false
|
||||||
*/
|
*/
|
||||||
function is_hex($hex) {
|
function is_hex($hex)
|
||||||
// regex is for weenies
|
{
|
||||||
$hex = strtolower(trim(ltrim($hex,"0")));
|
return ctype_xdigit($hex);
|
||||||
if (empty($hex)) { $hex = 0; };
|
}
|
||||||
$dec = hexdec($hex);
|
|
||||||
return ($hex == dechex($dec));
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user