From 8d875f3d022de874c68a6e9c096b0775c300dd21 Mon Sep 17 00:00:00 2001 From: Nikko Bautista Date: Wed, 24 Apr 2013 16:05:06 -0700 Subject: [PATCH] Composer-compatiblity; PHPDoc --- .gitignore | 3 + composer.json | 28 ++++++ lib/Chunked.php | 71 ------------- lib/HttpMethod.php | 37 ------- lib/HttpResponse.php | 73 -------------- lib/Unirest.php | 119 ---------------------- lib/Unirest/HttpMethod.php | 11 +++ lib/Unirest/HttpResponse.php | 79 +++++++++++++++ lib/Unirest/Unirest.php | 187 +++++++++++++++++++++++++++++++++++ 9 files changed, 308 insertions(+), 300 deletions(-) create mode 100644 composer.json delete mode 100755 lib/Chunked.php delete mode 100755 lib/HttpMethod.php delete mode 100644 lib/HttpResponse.php delete mode 100644 lib/Unirest.php create mode 100644 lib/Unirest/HttpMethod.php create mode 100644 lib/Unirest/HttpResponse.php create mode 100644 lib/Unirest/Unirest.php diff --git a/.gitignore b/.gitignore index e43b0f9..9667df9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .DS_Store +/vendor +composer.lock +composer.phar \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..df1bda7 --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name" : "mashape/unirest-php", + "description" : "Unirest PHP", + "keywords" : ["rest", "curl"], + "type" : "library", + "homepage" : "http://unireset.io#php", + "license" : "MIT", + "authors" : [ + { + "name" : "Mashape", + "email" : "hey@mashape.com", + "homepage" : "http://mashape.com" + } + ], + "require" : { + "php" : ">=5.3.0", + "ext-curl" : "*", + "ext-json" : "*" + }, + "autoload" : { + "psr-0" : { + "Unirest" : "lib" + } + }, + "support" : { + "email" : "hey@mashape.com" + } +} \ No newline at end of file diff --git a/lib/Chunked.php b/lib/Chunked.php deleted file mode 100755 index 2ba9241..0000000 --- a/lib/Chunked.php +++ /dev/null @@ -1,71 +0,0 @@ - \ No newline at end of file diff --git a/lib/HttpMethod.php b/lib/HttpMethod.php deleted file mode 100755 index a6a8f32..0000000 --- a/lib/HttpMethod.php +++ /dev/null @@ -1,37 +0,0 @@ - \ No newline at end of file diff --git a/lib/HttpResponse.php b/lib/HttpResponse.php deleted file mode 100644 index b864496..0000000 --- a/lib/HttpResponse.php +++ /dev/null @@ -1,73 +0,0 @@ -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) { - if (!empty($key) && substr($key, 0, 4) != "HTTP") { - $result[$key] = $value; - } else { - list ($key, $value) = explode(': ', $line); - } - } - } - return $result; - } -} - -?> diff --git a/lib/Unirest.php b/lib/Unirest.php deleted file mode 100644 index 864a8e6..0000000 --- a/lib/Unirest.php +++ /dev/null @@ -1,119 +0,0 @@ - $val) { - $key = trim(strtolower($key)); - if ($key == "user-agent" || key == "expect") continue; - $lowercaseHeaders[] = $key . ": " . $val; - } - $lowercaseHeaders[] = "user-agent: unirest-php/1.0"; - $lowercaseHeaders[] = "expect:"; - - $ch = curl_init(); - if ($httpMethod != HttpMethod::GET) { - curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); - curl_setopt ($ch, CURLOPT_POSTFIELDS, $body); - } - - curl_setopt ($ch, CURLOPT_URL , Unirest::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, $lowercaseHeaders); - curl_setopt ($ch, CURLOPT_HEADER, true); - curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false); - - $response = curl_exec($ch); - $error = curl_error($ch); - if ($error) { - throw new Exception($error); - } - - // 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) { - // Parse URL into pieces - $url_parsed = parse_url($url); - - // Build the basics bypassing notices - $scheme = $url_parsed['scheme'] . '://'; - $host = $url_parsed['host']; - $port = ( isset($url_parsed['port']) ? $url_parsed['port'] : null ); - $path = ( isset($url_parsed['path']) ? $url_parsed['path'] : null ); - $query = ( isset($url_parsed['query']) ? $url_parsed['query'] : null ); - - // Do we need to encode anything? - if ($query != null) { - // Break up the query into an array - parse_str($url_parsed['query'], $query_parsed); - // Encode and build query based on RFC 1738 - $query = '?'.http_build_query($query_parsed); - } - - // Return the completed URL - $result = $scheme . $host . $port . $path . $query; - return $result; - } - -} - -?> \ No newline at end of file diff --git a/lib/Unirest/HttpMethod.php b/lib/Unirest/HttpMethod.php new file mode 100644 index 0000000..3ee119b --- /dev/null +++ b/lib/Unirest/HttpMethod.php @@ -0,0 +1,11 @@ +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; + } + } + + /** + * Return a property of the response if it exists + * Possibilities include: + * - code + * - raw_body + * - body (if the response is json-decodable) + * - headers + * @param [type] $property [description] + * @return [type] [description] + */ + public function __get($property) + { + if (property_exists($this, $property)) { + return $this->$property; + } + } + + /** + * Set the properties of this object + * @param string $property The property name + * @param mixed $value The property value + */ + public function __set($property, $value) + { + if (property_exists($this, $property)) { + $this->$property = $value; + } + return $this; + } + + /** + * Retrieve the cURL response headers from the + * header string and convert it into an array + * @param string $headers header string from cURL response + * @return array headers in array form + */ + private function get_headers_from_curl_response($headers) + { + foreach (explode("\r\n", $headers) as $i => $line) { + if ($i !== 0) { + if (!empty($key) && substr($key, 0, 4) != "HTTP") { + $result[$key] = $value; + } else { + list ($key, $value) = explode(': ', $line); + } + } + } + + return $result; + } +} diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php new file mode 100644 index 0000000..a0b30b0 --- /dev/null +++ b/lib/Unirest/Unirest.php @@ -0,0 +1,187 @@ + $val) { + $key = trim(strtolower($key)); + if ($key == "user-agent" || key == "expect") continue; + $lowercaseHeaders[] = $key . ": " . $val; + } + $lowercaseHeaders[] = "user-agent: unirest-php/1.0"; + $lowercaseHeaders[] = "expect:"; + + $ch = curl_init(); + if ($httpMethod != HttpMethod::GET) { + curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); + curl_setopt ($ch, CURLOPT_POSTFIELDS, $body); + } + + curl_setopt ($ch, CURLOPT_URL , Unirest::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, $lowercaseHeaders); + curl_setopt ($ch, CURLOPT_HEADER, true); + curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false); + + $response = curl_exec($ch); + $error = curl_error($ch); + if ($error) { + throw new \Exception($error); + } + + // 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); + } + + /** + * Ensure that a URL is encoded and safe to use with cURL + * @param string $url URL to encode + * @return string Encoded URL + */ + private static function encodeUrl($url) + { + // Parse URL into pieces + $url_parsed = parse_url($url); + + // Build the basics bypassing notices + $scheme = $url_parsed['scheme'] . '://'; + $host = $url_parsed['host']; + $port = (isset($url_parsed['port']) ? $url_parsed['port'] : null ); + $path = (isset($url_parsed['path']) ? $url_parsed['path'] : null ); + $query = (isset($url_parsed['query']) ? $url_parsed['query'] : null ); + + // Do we need to encode anything? + if ($query != null) { + // Break up the query into an array + parse_str($url_parsed['query'], $query_parsed); + // Encode and build query based on RFC 1738 + $query = '?'.http_build_query($query_parsed); + } + + // Return the completed URL + $result = $scheme . $host . $port . $path . $query; + return $result; + } + +} + +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)); +} \ No newline at end of file