From 2b843de34b77738d4ad56311e0412bf1171a0d09 Mon Sep 17 00:00:00 2001 From: jasir Date: Tue, 7 Jan 2014 12:29:19 +0100 Subject: [PATCH 01/18] typo in doc --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4043a6c..2a974f1 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ Unirest::clearDefaultHeaders(); You can explicitly enable or disable SSL certificate validation when consuming an SSL protected endpoint: ```php -Unirest::verifiyPeer(false); // Disables SSL cert validation +Unirest::verifyPeer(false); // Disables SSL cert validation ``` By default is `true`. From bd4975cb3c5e64093130ec20608d4c4da52a32a3 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 8 Jan 2014 18:14:42 -0800 Subject: [PATCH 02/18] closes #28 --- lib/Unirest/Unirest.php | 22 +++++++++++++++++++++- test/Unirest/UnirestTest.php | 24 ++++++++++++++++++++++++ test/Unirest/test_upload.txt | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/Unirest/test_upload.txt diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index 855aa5f..248282f 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -113,6 +113,25 @@ return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password); } + /** + * This function is useful for serializing multidimensional arrays, and avoid getting + * the "Array to string conversion" notice + */ + private static function http_build_query_for_curl( $arrays, &$new = array(), $prefix = null ) { + if ( is_object( $arrays ) ) { + $arrays = get_object_vars( $arrays ); + } + + foreach ( $arrays AS $key => $value ) { + $k = isset( $prefix ) ? $prefix . '[' . $key . ']' : $key; + if ( is_array( $value ) OR is_object( $value ) ) { + Unirest::http_build_query_for_curl( $value, $new, $k ); + } else { + $new[$k] = $value; + } + } + } + /** * Send a cURL request * @param string $httpMethod HTTP method to use (based off \Unirest\HttpMethod constants) @@ -141,7 +160,8 @@ $ch = curl_init(); if ($httpMethod != HttpMethod::GET) { curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); - curl_setopt ($ch, CURLOPT_POSTFIELDS, $body); + Unirest::http_build_query_for_curl($body, $postBody); + curl_setopt ($ch, CURLOPT_POSTFIELDS, $postBody); } else if (is_array($body)) { if (strpos($url,'?') !== false) { $url .= "&"; diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index 8f3977c..a259740 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -31,6 +31,30 @@ class UnirestTest extends UnitTestCase $this->assertEqual("thefosk", $form->nick); } + public function testUpload() { + $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), + array( + "file" => "@" . dirname(__FILE__) . "/test_upload.txt" + ) + ); + $this->assertEqual(200, $response->code); + + $files = $response->body->files; + $this->assertEqual("This is a test", $files->file); + } + + public function testPostMultidimensionalArray() + { + $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), + array('key'=>'value','items'=>array('item1','item2'))); + + $this->assertEqual(200, $response->code); + + $form = $response->body->form; + $this->assertEqual("item1", $form->{"items[0]"}); + $this->assertEqual("item2", $form->{"items[1]"}); + } + public function testPut() { $response = Unirest::put("http://httpbin.org/put", array( "Accept" => "application/json" ), diff --git a/test/Unirest/test_upload.txt b/test/Unirest/test_upload.txt new file mode 100644 index 0000000..793aa68 --- /dev/null +++ b/test/Unirest/test_upload.txt @@ -0,0 +1 @@ +This is a test \ No newline at end of file From bbdc2485502e24efc14cbf2462dd46a52480e41e Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 8 Jan 2014 18:49:23 -0800 Subject: [PATCH 03/18] closes #26 --- lib/Unirest/Unirest.php | 25 +++++++++++++++----- test/Unirest/UnirestTest.php | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index 248282f..b32fefe 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -168,11 +168,13 @@ } else { $url .= "?"; } + Unirest::http_build_query_for_curl($body, $postBody); + $url .= http_build_query($postBody); - foreach ($body as $parameter => $val) { - $url .= $parameter . "=" . $val . "&"; - } - $url = substr($url, 0, strlen($url) - 1); + //foreach ($body as $parameter => $val) { + // $url .= $parameter . "=" . $val . "&"; + //} + //$url = substr($url, 0, strlen($url) - 1); } curl_setopt ($ch, CURLOPT_URL, Unirest::encodeUrl($url)); @@ -206,6 +208,18 @@ return new HttpResponse($httpCode, $body, $header); } + private static function getArrayFromQuerystring($querystring) { + $pairs = explode("&", $querystring); + $vars = array(); + foreach ($pairs as $pair) { + $nv = explode("=", $pair); + $name = $nv[0]; + $value = $nv[1]; + $vars[$name] = $value; + } + return $vars; + } + /** * Ensure that a URL is encoded and safe to use with cURL * @param string $url URL to encode @@ -222,8 +236,7 @@ $query = (isset($url_parsed['query']) ? $url_parsed['query'] : null); if ($query != null) { - parse_str($url_parsed['query'], $query_parsed); - $query = '?' . http_build_query($query_parsed); + $query = '?' . http_build_query(Unirest::getArrayFromQuerystring($url_parsed['query'])); } if ($port && $port[0] != ":") diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index a259740..68b05ff 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -16,6 +16,36 @@ class UnirestTest extends UnitTestCase $this->assertEqual("thefosk", $args->nick); } + public function testGetWithDots() + { + $response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), + array( + "user.name" => "Mark", + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + $this->assertEqual("Mark", $args->{"user.name"}); + $this->assertEqual("thefosk", $args->nick); + } + + public function testGetWithDots2() + { + $response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), + array( + "user.name" => "Mark Bond", + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + $this->assertEqual("Mark+Bond", $args->{"user.name"}); + $this->assertEqual("thefosk", $args->nick); + } + public function testPost() { $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), @@ -31,6 +61,21 @@ class UnirestTest extends UnitTestCase $this->assertEqual("thefosk", $form->nick); } + public function testPostWithDots() + { + $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), + array( + "user.name" => "Mark", + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + + $form = $response->body->form; + $this->assertEqual("Mark", $form->{"user.name"}); + $this->assertEqual("thefosk", $form->nick); + } + public function testUpload() { $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), array( From 7ea2f5f750a289532e5773069c81481ec524df14 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 8 Jan 2014 18:50:12 -0800 Subject: [PATCH 04/18] closes #26 --- test/Unirest/UnirestTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index 68b05ff..b97d042 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -16,6 +16,20 @@ class UnirestTest extends UnitTestCase $this->assertEqual("thefosk", $args->nick); } + public function testGetMultidimensionalArray() + { + $response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), + array('key'=>'value','items'=>array('item1','item2'))); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + print_r($args); + + $this->assertEqual("Mark", $args->{"user.name"}); + $this->assertEqual("thefosk", $args->nick); + } + public function testGetWithDots() { $response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), From bf865a5c3065d5b3caa6d1bd1299c1728d4f7c12 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 8 Jan 2014 18:58:12 -0800 Subject: [PATCH 05/18] tests --- test/Unirest/UnirestTest.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index b97d042..c9d19aa 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -24,10 +24,10 @@ class UnirestTest extends UnitTestCase $this->assertEqual(200, $response->code); $args = $response->body->args; - print_r($args); - $this->assertEqual("Mark", $args->{"user.name"}); - $this->assertEqual("thefosk", $args->nick); + $this->assertEqual("value", $args->key); + $this->assertEqual("item1", $args->{"items%5B0%5D"}); + $this->assertEqual("item2", $args->{"items%5B1%5D"}); } public function testGetWithDots() @@ -93,6 +93,7 @@ class UnirestTest extends UnitTestCase public function testUpload() { $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), array( + "name" => "Mark", "file" => "@" . dirname(__FILE__) . "/test_upload.txt" ) ); @@ -100,6 +101,9 @@ class UnirestTest extends UnitTestCase $files = $response->body->files; $this->assertEqual("This is a test", $files->file); + + $form = $response->body->form; + $this->assertEqual("Mark", $form->name); } public function testPostMultidimensionalArray() @@ -110,6 +114,7 @@ class UnirestTest extends UnitTestCase $this->assertEqual(200, $response->code); $form = $response->body->form; + $this->assertEqual("value", $form->key); $this->assertEqual("item1", $form->{"items[0]"}); $this->assertEqual("item2", $form->{"items[1]"}); } From 1504e81ff11562b9cdda6ac57c4902e63628bbe6 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 8 Jan 2014 19:05:22 -0800 Subject: [PATCH 06/18] supporting curl_file_create` --- README.md | 4 +++- lib/Unirest/Unirest.php | 12 ++++++++++++ test/Unirest/UnirestTest.php | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2a974f1..530df99 100644 --- a/README.md +++ b/README.md @@ -71,10 +71,12 @@ To upload files in a multipart form representation simply place an `@` symbol be ```php $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), array( - "file" => "@/tmp/file.txt" + "file" => Unirest::file("/tmp/file.txt") ) ); ``` + + Use the value of `Unirest::file($path)` to assign a file to a parameter. ### Custom Entity Body Sending a custom body such as a JSON Object rather than a string or form style parameters we utilize json_encode for the body: diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index b32fefe..ef3fbf2 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -113,6 +113,18 @@ return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password); } + /** + * Prepares a file for upload. To be used inside the parameters declaration for a request. + * @param string $path The file path + */ + public static function file($path) { + if (function_exists("curl_file_create")) { + return curl_file_create($path); + } else { + return "@" . $path; + } + } + /** * This function is useful for serializing multidimensional arrays, and avoid getting * the "Array to string conversion" notice diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index c9d19aa..e6eca2b 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -94,7 +94,7 @@ class UnirestTest extends UnitTestCase $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), array( "name" => "Mark", - "file" => "@" . dirname(__FILE__) . "/test_upload.txt" + "file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt") ) ); $this->assertEqual(200, $response->code); From 257ad85e6b2cb00dfafa3c41be29261affcb82d5 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 8 Jan 2014 19:06:31 -0800 Subject: [PATCH 07/18] readme --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 530df99..9df478f 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ $response->raw_body; // Unparsed body ### File Uploads -To upload files in a multipart form representation simply place an `@` symbol before the path: +To upload files in a multipart form representation use the return value of `Unirest::file($path)` as the value of a parameter: ```php $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), @@ -75,8 +75,6 @@ $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "applica ) ); ``` - - Use the value of `Unirest::file($path)` to assign a file to a parameter. ### Custom Entity Body Sending a custom body such as a JSON Object rather than a string or form style parameters we utilize json_encode for the body: From f5fee2dc46b2a566600b15f154e72cae7f4181c7 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 8 Jan 2014 19:07:05 -0800 Subject: [PATCH 08/18] readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9df478f..a35e297 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ To upload files in a multipart form representation use the return value of `Unir ```php $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), array( - "file" => Unirest::file("/tmp/file.txt") + "file" => Unirest::file("/tmp/file.txt") // Tells Unirest where the file is located ) ); ``` From 646e24aefec139e5d104e6804a6630a7ae7339bd Mon Sep 17 00:00:00 2001 From: Sam Sullivan Date: Mon, 13 Jan 2014 23:21:04 -0600 Subject: [PATCH 09/18] Check if $body is traversable before recursively sanitizing (allows for raw JSON body) Add testRawPost() --- lib/Unirest/Unirest.php | 10 +++++++--- test/Unirest/UnirestTest.php | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index ef3fbf2..0edaf91 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -171,9 +171,13 @@ $ch = curl_init(); if ($httpMethod != HttpMethod::GET) { - curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); - Unirest::http_build_query_for_curl($body, $postBody); - curl_setopt ($ch, CURLOPT_POSTFIELDS, $postBody); + curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); + if( is_array($body) || $body instanceof Traversable ) { + Unirest::http_build_query_for_curl($body, $postBody); + curl_setopt ($ch, CURLOPT_POSTFIELDS, $postBody); + } else { + curl_setopt ($ch, CURLOPT_POSTFIELDS, $body); + } } else if (is_array($body)) { if (strpos($url,'?') !== false) { $url .= "&"; diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index e6eca2b..faffaed 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -90,6 +90,19 @@ class UnirestTest extends UnitTestCase $this->assertEqual("thefosk", $form->nick); } + public function testRawPost() + { + $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), + json_encode(array( + "author" => "Sam Sullivan" + ))); + + $this->assertEqual(200, $response->code); + + $json = $response->body->json; + $this->assertEqual("Sam Sullivan", $json->author); + } + public function testUpload() { $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), array( From 8b184d99110d19893d69f018d3fbb64c0e25d23a Mon Sep 17 00:00:00 2001 From: thefosk Date: Tue, 14 Jan 2014 10:29:17 -0800 Subject: [PATCH 10/18] comments deleted --- lib/Unirest/Unirest.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index ef3fbf2..f9e2e09 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -182,11 +182,6 @@ } Unirest::http_build_query_for_curl($body, $postBody); $url .= http_build_query($postBody); - - //foreach ($body as $parameter => $val) { - // $url .= $parameter . "=" . $val . "&"; - //} - //$url = substr($url, 0, strlen($url) - 1); } curl_setopt ($ch, CURLOPT_URL, Unirest::encodeUrl($url)); From f3af9e81967489a8256829906ce4524d41cbf250 Mon Sep 17 00:00:00 2001 From: thefosk Date: Tue, 14 Jan 2014 10:34:19 -0800 Subject: [PATCH 11/18] fix --- lib/Unirest/Unirest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index 440f9ae..c973bd6 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -171,13 +171,13 @@ $ch = curl_init(); if ($httpMethod != HttpMethod::GET) { - curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); - if( is_array($body) || $body instanceof Traversable ) { - Unirest::http_build_query_for_curl($body, $postBody); - curl_setopt ($ch, CURLOPT_POSTFIELDS, $postBody); - } else { - curl_setopt ($ch, CURLOPT_POSTFIELDS, $body); - } + curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); + if( is_array($body) || $body instanceof Traversable ) { + Unirest::http_build_query_for_curl($body, $postBody); + curl_setopt ($ch, CURLOPT_POSTFIELDS, $postBody); + } else { + curl_setopt ($ch, CURLOPT_POSTFIELDS, $body); + } } else if (is_array($body)) { if (strpos($url,'?') !== false) { $url .= "&"; From cabef82a6d7d045a23833ce0550e1e883de68a9f Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 19 Feb 2014 11:18:20 -0800 Subject: [PATCH 12/18] debugging failing test --- test/Unirest/UnirestTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index faffaed..9303f27 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -104,6 +104,9 @@ class UnirestTest extends UnitTestCase } public function testUpload() { + + var_dump(file_get_contents(dirname(__FILE__) . "/test_upload.txt")); + $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), array( "name" => "Mark", From e6682a5ad1e103606a6d9d44efdf6d9cb619410d Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 19 Feb 2014 11:20:52 -0800 Subject: [PATCH 13/18] more debugging --- test/Unirest/UnirestTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index 9303f27..2cdc084 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -116,6 +116,8 @@ class UnirestTest extends UnitTestCase $this->assertEqual(200, $response->code); $files = $response->body->files; + var_dump($response->body); + var_dump($files); $this->assertEqual("This is a test", $files->file); $form = $response->body->form; From 2fb13154e29e4ed58d20ddf64c66502b92f9eba5 Mon Sep 17 00:00:00 2001 From: thefosk Date: Tue, 18 Mar 2014 11:15:38 -0700 Subject: [PATCH 14/18] formatting --- lib/Unirest/HttpMethod.php | 23 +- lib/Unirest/HttpResponse.php | 142 ++++---- lib/Unirest/Unirest.php | 608 ++++++++++++++++++----------------- test/Unirest.php | 19 +- test/Unirest/UnirestTest.php | 487 +++++++++++++++------------- 5 files changed, 654 insertions(+), 625 deletions(-) diff --git a/lib/Unirest/HttpMethod.php b/lib/Unirest/HttpMethod.php index 9aaec19..9252405 100644 --- a/lib/Unirest/HttpMethod.php +++ b/lib/Unirest/HttpMethod.php @@ -1,15 +1,14 @@ 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; + $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; + } + } + + /** + * Return a property of the response if it exists. + * Possibilities include: code, raw_body, headers, body (if the response is json-decodable) + * @return mixed + */ + 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 + */ + private function get_headers_from_curl_response($headers) + { + $headers = explode("\r\n", $headers); + array_shift($headers); + + foreach ($headers as $line) { + if (strstr($line, ': ')) { + list($key, $value) = explode(': ', $line); + $result[$key] = $value; } } - - /** - * Return a property of the response if it exists. - * Possibilities include: code, raw_body, headers, body (if the response is json-decodable) - * @return mixed - */ - 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 - */ - private function get_headers_from_curl_response($headers) - { - $headers = explode("\r\n", $headers); - array_shift($headers); - - foreach ($headers as $line) { - if (strstr($line, ': ')) { - list ($key, $value) = explode(': ', $line); - $result[$key] = $value; - } - } - - return $result; - } - - } \ No newline at end of file + + return $result; + } + +} \ No newline at end of file diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index c973bd6..4f75700 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -1,309 +1,317 @@ $value ) { - $k = isset( $prefix ) ? $prefix . '[' . $key . ']' : $key; - if ( is_array( $value ) OR is_object( $value ) ) { - Unirest::http_build_query_for_curl( $value, $new, $k ); - } else { - $new[$k] = $value; - } - } - } - - /** - * Send a cURL request - * @param string $httpMethod HTTP method to use (based off \Unirest\HttpMethod constants) - * @param string $url URL to send the request to - * @param mixed $body request body - * @param array $headers additional headers to send - * @param string $username Basic Authentication username - * @param string $password Basic Authentication password - * @throws Exception if a cURL error occurs - * @return HttpResponse - */ - private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL) - { - if ($headers == NULL) $headers = array(); - $lowercaseHeaders = array(); - foreach ($headers as $key => $val) { - $lowercaseHeaders[] = Unirest::getHeader($key, $val); - } - foreach (Unirest::$defaultHeaders as $key => $val) { - $lowercaseHeaders[] = Unirest::getHeader($key, $val); - } - - $lowercaseHeaders[] = "user-agent: unirest-php/1.1"; - $lowercaseHeaders[] = "expect:"; - - $ch = curl_init(); - if ($httpMethod != HttpMethod::GET) { - curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); - if( is_array($body) || $body instanceof Traversable ) { - Unirest::http_build_query_for_curl($body, $postBody); - curl_setopt ($ch, CURLOPT_POSTFIELDS, $postBody); - } else { - curl_setopt ($ch, CURLOPT_POSTFIELDS, $body); - } - } else if (is_array($body)) { - if (strpos($url,'?') !== false) { - $url .= "&"; - } else { - $url .= "?"; - } - Unirest::http_build_query_for_curl($body, $postBody); - $url .= http_build_query($postBody); - } - - 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, Unirest::$verifyPeer); - curl_setopt ($ch, CURLOPT_ENCODING, ""); // If an empty string, "", is set, a header containing all supported encoding types is sent. - if (Unirest::$socketTimeout != null) { - curl_setopt ($ch, CURLOPT_TIMEOUT, Unirest::$socketTimeout); - } - if (!empty($username)) { - curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . ((empty($password)) ? "" : $password)); - } - - $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 getArrayFromQuerystring($querystring) { - $pairs = explode("&", $querystring); - $vars = array(); - foreach ($pairs as $pair) { - $nv = explode("=", $pair); - $name = $nv[0]; - $value = $nv[1]; - $vars[$name] = $value; - } - return $vars; - } - - /** - * Ensure that a URL is encoded and safe to use with cURL - * @param string $url URL to encode - * @return string - */ - private static function encodeUrl($url) - { - $url_parsed = parse_url($url); - - $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); - - if ($query != null) { - $query = '?' . http_build_query(Unirest::getArrayFromQuerystring($url_parsed['query'])); - } - - if ($port && $port[0] != ":") - $port = ":" . $port; - - $result = $scheme . $host . $port . $path . $query; - return $result; - } - - private static function getHeader($key, $val) { - $key = trim(strtolower($key)); - if ($key == "user-agent" || $key == "expect") continue; - return $key . ": " . $val; - } - - } - - if (!function_exists('http_chunked_decode')) { - /** - * Dechunk an http 'transfer-encoding: chunked' message - * @param string $chunk the encoded message - * @return string the decoded message - */ - 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; - } - } +use Unirest\HttpMethod; +use Unirest\HttpResponse; +class Unirest +{ + + private static $verifyPeer = true; + private static $socketTimeout = null; + private static $defaultHeaders = array(); + /** - * determine if a string can represent a number in hexadecimal - * @link http://uk1.php.net/ctype_xdigit - * @param string $hex - * @return boolean true if the string is a hex, otherwise false + * Verify SSL peer + * @param bool $enabled enable SSL verification, by default is true */ - function is_hex($hex) + public static function verifyPeer($enabled) { - return ctype_xdigit($hex); + Unirest::$verifyPeer = $enabled; } + /** + * Set a timeout + * @param integer $seconds timeout value in seconds + */ + public static function timeout($seconds) + { + Unirest::$socketTimeout = $seconds; + } + + /** + * Set a new default header to send on every request + * @param string $name header name + * @param string $value header value + */ + public static function defaultHeader($name, $value) + { + Unirest::$defaultHeaders[$name] = $value; + } + + /** + * Clear all the default headers + */ + public static function clearDefaultHeaders() + { + Unirest::$defaultHeaders = array(); + } + + /** + * Send a GET request to a URL + * @param string $url URL to send the GET request to + * @param array $headers additional headers to send + * @param mixed $parameters parameters to send in the querystring + * @param string $username Basic Authentication username + * @param string $password Basic Authentication password + * @return string|stdObj response string or stdObj if response is json-decodable + */ + public static function get($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL) + { + return Unirest::request(HttpMethod::GET, $url, $parameters, $headers, $username, $password); + } + + /** + * Send POST request to a URL + * @param string $url URL to send the POST request to + * @param array $headers additional headers to send + * @param mixed $body POST body data + * @param string $username Basic Authentication username + * @param string $password Basic Authentication password + * @return string|stdObj response string or stdObj if response is json-decodable + */ + public static function post($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL) + { + return Unirest::request(HttpMethod::POST, $url, $body, $headers, $username, $password); + } + + /** + * Send DELETE request to a URL + * @param string $url URL to send the DELETE request to + * @param array $headers additional headers to send + * @param mixed $body DELETE body data + * @param string $username Basic Authentication username + * @param string $password Basic Authentication password + * @return string|stdObj response string or stdObj if response is json-decodable + */ + public static function delete($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL) + { + return Unirest::request(HttpMethod::DELETE, $url, $body, $headers, $username, $password); + } + + /** + * Send PUT request to a URL + * @param string $url URL to send the PUT request to + * @param array $headers additional headers to send + * @param mixed $body PUT body data + * @param string $username Basic Authentication username + * @param string $password Basic Authentication password + * @return string|stdObj response string or stdObj if response is json-decodable + */ + public static function put($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL) + { + return Unirest::request(HttpMethod::PUT, $url, $body, $headers, $username, $password); + } + + /** + * Send PATCH request to a URL + * @param string $url URL to send the PATCH request to + * @param array $headers additional headers to send + * @param mixed $body PATCH body data + * @param string $username Basic Authentication username + * @param string $password Basic Authentication password + * @return string|stdObj response string or stdObj if response is json-decodable + */ + public static function patch($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL) + { + return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password); + } + + /** + * Prepares a file for upload. To be used inside the parameters declaration for a request. + * @param string $path The file path + */ + public static function file($path) + { + if (function_exists("curl_file_create")) { + return curl_file_create($path); + } else { + return "@" . $path; + } + } + + /** + * This function is useful for serializing multidimensional arrays, and avoid getting + * the "Array to string conversion" notice + */ + private static function http_build_query_for_curl($arrays, &$new = array(), $prefix = null) + { + if (is_object($arrays)) { + $arrays = get_object_vars($arrays); + } + + foreach ($arrays AS $key => $value) { + $k = isset($prefix) ? $prefix . '[' . $key . ']' : $key; + if (is_array($value) OR is_object($value)) { + Unirest::http_build_query_for_curl($value, $new, $k); + } else { + $new[$k] = $value; + } + } + } + + /** + * Send a cURL request + * @param string $httpMethod HTTP method to use (based off \Unirest\HttpMethod constants) + * @param string $url URL to send the request to + * @param mixed $body request body + * @param array $headers additional headers to send + * @param string $username Basic Authentication username + * @param string $password Basic Authentication password + * @throws Exception if a cURL error occurs + * @return HttpResponse + */ + private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL) + { + if ($headers == NULL) + $headers = array(); + $lowercaseHeaders = array(); + foreach ($headers as $key => $val) { + $lowercaseHeaders[] = Unirest::getHeader($key, $val); + } + foreach (Unirest::$defaultHeaders as $key => $val) { + $lowercaseHeaders[] = Unirest::getHeader($key, $val); + } + + $lowercaseHeaders[] = "user-agent: unirest-php/1.1"; + $lowercaseHeaders[] = "expect:"; + + $ch = curl_init(); + if ($httpMethod != HttpMethod::GET) { + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); + if (is_array($body) || $body instanceof Traversable) { + Unirest::http_build_query_for_curl($body, $postBody); + curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody); + } else { + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + } + } else if (is_array($body)) { + if (strpos($url, '?') !== false) { + $url .= "&"; + } else { + $url .= "?"; + } + Unirest::http_build_query_for_curl($body, $postBody); + $url .= http_build_query($postBody); + } + + 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, Unirest::$verifyPeer); + curl_setopt($ch, CURLOPT_ENCODING, ""); // If an empty string, "", is set, a header containing all supported encoding types is sent. + if (Unirest::$socketTimeout != null) { + curl_setopt($ch, CURLOPT_TIMEOUT, Unirest::$socketTimeout); + } + if (!empty($username)) { + curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . ((empty($password)) ? "" : $password)); + } + + $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 getArrayFromQuerystring($querystring) + { + $pairs = explode("&", $querystring); + $vars = array(); + foreach ($pairs as $pair) { + $nv = explode("=", $pair); + $name = $nv[0]; + $value = $nv[1]; + $vars[$name] = $value; + } + return $vars; + } + + /** + * Ensure that a URL is encoded and safe to use with cURL + * @param string $url URL to encode + * @return string + */ + private static function encodeUrl($url) + { + $url_parsed = parse_url($url); + + $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); + + if ($query != null) { + $query = '?' . http_build_query(Unirest::getArrayFromQuerystring($url_parsed['query'])); + } + + if ($port && $port[0] != ":") + $port = ":" . $port; + + $result = $scheme . $host . $port . $path . $query; + return $result; + } + + private static function getHeader($key, $val) + { + $key = trim(strtolower($key)); + if ($key == "user-agent" || $key == "expect") + continue; + return $key . ": " . $val; + } + +} + +if (!function_exists('http_chunked_decode')) { + /** + * Dechunk an http 'transfer-encoding: chunked' message + * @param string $chunk the encoded message + * @return string the decoded message + */ + 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 + * @link http://uk1.php.net/ctype_xdigit + * @param string $hex + * @return boolean true if the string is a hex, otherwise false + */ +function is_hex($hex) +{ + return ctype_xdigit($hex); +} + ?> \ No newline at end of file diff --git a/test/Unirest.php b/test/Unirest.php index 08b96e5..031c389 100644 --- a/test/Unirest.php +++ b/test/Unirest.php @@ -1,23 +1,20 @@ , and either install it ". - "in your PHP include_path or put it in the test/ directory.\n"; - exit(1); + echo "MISSING DEPENDENCY: The Unirest-PHP test cases depend on SimpleTest. " . "Download it at , and either install it " . "in your PHP include_path or put it in the test/ directory.\n"; + exit(1); } // Throw an exception on any error -function exception_error_handler($errno, $errstr, $errfile, $errline) { - throw new ErrorException($errstr, $errno, 0, $errfile, $errline); +function exception_error_handler($errno, $errstr, $errfile, $errline) +{ + throw new ErrorException($errstr, $errno, 0, $errfile, $errline); } set_error_handler('exception_error_handler'); diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index 2cdc084..3b3dd07 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -2,242 +2,267 @@ class UnirestTest extends UnitTestCase { - public function testGet() - { - $response = Unirest::get("http://httpbin.org/get?name=Mark", array( "Accept" => "application/json" ), - array( - "nick" => "thefosk" - )); - - $this->assertEqual(200, $response->code); - - $args = $response->body->args; - $this->assertEqual("Mark", $args->name); - $this->assertEqual("thefosk", $args->nick); - } - - public function testGetMultidimensionalArray() - { - $response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), - array('key'=>'value','items'=>array('item1','item2'))); - - $this->assertEqual(200, $response->code); - - $args = $response->body->args; - - $this->assertEqual("value", $args->key); - $this->assertEqual("item1", $args->{"items%5B0%5D"}); - $this->assertEqual("item2", $args->{"items%5B1%5D"}); - } - - public function testGetWithDots() - { - $response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), - array( - "user.name" => "Mark", - "nick" => "thefosk" - )); - - $this->assertEqual(200, $response->code); - - $args = $response->body->args; - $this->assertEqual("Mark", $args->{"user.name"}); - $this->assertEqual("thefosk", $args->nick); - } - - public function testGetWithDots2() - { - $response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), - array( - "user.name" => "Mark Bond", - "nick" => "thefosk" - )); - - $this->assertEqual(200, $response->code); + public function testGet() + { + $response = Unirest::get("http://httpbin.org/get?name=Mark", array( + "Accept" => "application/json" + ), array( + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + $this->assertEqual("Mark", $args->name); + $this->assertEqual("thefosk", $args->nick); + } + + public function testGetMultidimensionalArray() + { + $response = Unirest::get("http://httpbin.org/get", array( + "Accept" => "application/json" + ), array( + 'key' => 'value', + 'items' => array( + 'item1', + 'item2' + ) + )); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + + $this->assertEqual("value", $args->key); + $this->assertEqual("item1", $args->{"items%5B0%5D"}); + $this->assertEqual("item2", $args->{"items%5B1%5D"}); + } + + public function testGetWithDots() + { + $response = Unirest::get("http://httpbin.org/get", array( + "Accept" => "application/json" + ), array( + "user.name" => "Mark", + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + $this->assertEqual("Mark", $args->{"user.name"}); + $this->assertEqual("thefosk", $args->nick); + } + + public function testGetWithDots2() + { + $response = Unirest::get("http://httpbin.org/get", array( + "Accept" => "application/json" + ), array( + "user.name" => "Mark Bond", + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + $this->assertEqual("Mark+Bond", $args->{"user.name"}); + $this->assertEqual("thefosk", $args->nick); + } + + public function testPost() + { + $response = Unirest::post("http://httpbin.org/post", array( + "Accept" => "application/json" + ), array( + "name" => "Mark", + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + + $form = $response->body->form; + $this->assertEqual("Mark", $form->name); + $this->assertEqual("thefosk", $form->nick); + } + + public function testPostWithDots() + { + $response = Unirest::post("http://httpbin.org/post", array( + "Accept" => "application/json" + ), array( + "user.name" => "Mark", + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + + $form = $response->body->form; + $this->assertEqual("Mark", $form->{"user.name"}); + $this->assertEqual("thefosk", $form->nick); + } - $args = $response->body->args; - $this->assertEqual("Mark+Bond", $args->{"user.name"}); - $this->assertEqual("thefosk", $args->nick); - } - - public function testPost() - { - $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), - array( - "name" => "Mark", - "nick" => "thefosk" - )); - - $this->assertEqual(200, $response->code); - - $form = $response->body->form; - $this->assertEqual("Mark", $form->name); - $this->assertEqual("thefosk", $form->nick); - } - - public function testPostWithDots() - { - $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), - array( - "user.name" => "Mark", - "nick" => "thefosk" - )); - - $this->assertEqual(200, $response->code); - - $form = $response->body->form; - $this->assertEqual("Mark", $form->{"user.name"}); - $this->assertEqual("thefosk", $form->nick); - } - public function testRawPost() { - $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), - json_encode(array( - "author" => "Sam Sullivan" - ))); - + $response = Unirest::post("http://httpbin.org/post", array( + "Accept" => "application/json" + ), json_encode(array( + "author" => "Sam Sullivan" + ))); + $this->assertEqual(200, $response->code); - + $json = $response->body->json; $this->assertEqual("Sam Sullivan", $json->author); } - - public function testUpload() { - - var_dump(file_get_contents(dirname(__FILE__) . "/test_upload.txt")); - - $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), - array( - "name" => "Mark", - "file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt") - ) - ); - $this->assertEqual(200, $response->code); - - $files = $response->body->files; - var_dump($response->body); - var_dump($files); - $this->assertEqual("This is a test", $files->file); - - $form = $response->body->form; - $this->assertEqual("Mark", $form->name); - } - - public function testPostMultidimensionalArray() - { - $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), - array('key'=>'value','items'=>array('item1','item2'))); - - $this->assertEqual(200, $response->code); - - $form = $response->body->form; - $this->assertEqual("value", $form->key); - $this->assertEqual("item1", $form->{"items[0]"}); - $this->assertEqual("item2", $form->{"items[1]"}); - } - - public function testPut() - { - $response = Unirest::put("http://httpbin.org/put", array( "Accept" => "application/json" ), - array( - "name" => "Mark", - "nick" => "thefosk" - )); - - $this->assertEqual(200, $response->code); - - $form = $response->body->form; - $this->assertEqual("Mark", $form->name); - $this->assertEqual("thefosk", $form->nick); - } - - public function testPatch() - { - $response = Unirest::patch("http://httpbin.org/patch", array( "Accept" => "application/json" ), - array( - "name" => "Mark", - "nick" => "thefosk" - )); - - $this->assertEqual(200, $response->code); - - $form = $response->body->form; - $this->assertEqual("Mark", $form->name); - $this->assertEqual("thefosk", $form->nick); - } - - public function testDelete() - { - $response = Unirest::delete("http://httpbin.org/delete", array( "Accept" => "application/json", "Content-Type" => "application/x-www-form-urlencoded" ), - array( - "name" => "Mark", - "nick" => "thefosk" - )); - - $this->assertEqual(200, $response->code); - $data = $response->body->data; - $this->assertFalse(empty($data)); - } - - public function testTimeoutFail() - { - Unirest::timeout(1); - - $this->expectException(); - $response = Unirest::get("http://httpbin.org/delay/3"); - - Unirest::timeout(null); // Cleaning timeout for the other tests - } - - public function testTimeoutSuccess() - { - Unirest::timeout(3); - - $response = Unirest::get("http://httpbin.org/delay/1"); - $this->assertEqual(200, $response->code); - - Unirest::timeout(null); // Cleaning timeout for the other tests - } - - public function testDefaultHeader() - { - Unirest::defaultHeader("Hello", "custom"); - $response = Unirest::get("http://httpbin.org/get"); - - $this->assertEqual(200, $response->code); - $headers = $response->body->headers; - $properties = get_object_vars($headers); - $this->assertTrue(array_key_exists("Hello", $properties)); - $this->assertEqual("custom", $headers->Hello); - $response = Unirest::get("http://httpbin.org/get"); - - $this->assertEqual(200, $response->code); - $headers = $response->body->headers; - $properties = get_object_vars($headers); - $this->assertTrue(array_key_exists("Hello", $properties)); - $this->assertEqual("custom", $headers->Hello); - Unirest::clearDefaultHeaders(); - $response = Unirest::get("http://httpbin.org/get"); - - $this->assertEqual(200, $response->code); - $headers = $response->body->headers; - $properties = get_object_vars($headers); - $this->assertFalse(array_key_exists("Hello", $properties)); - } - - public function testGzip() - { - $response = Unirest::get("http://httpbin.org/gzip"); - $args = $response->body; - $this->assertEqual(true, $args->gzipped); - } - - public function testBasicAuthentication() - { - $response = Unirest::get("http://httpbin.org/get", null, null, "user", "password"); - $headers = $response->body->headers; - $this->assertEqual("Basic dXNlcjpwYXNzd29yZA==", $headers->Authorization); - } - + + public function testUpload() + { + + var_dump(file_get_contents(dirname(__FILE__) . "/test_upload.txt")); + + $response = Unirest::post("http://httpbin.org/post", array( + "Accept" => "application/json" + ), array( + "name" => "Mark", + "file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt") + )); + $this->assertEqual(200, $response->code); + + $files = $response->body->files; + var_dump($response->body); + var_dump($files); + $this->assertEqual("This is a test", $files->file); + + $form = $response->body->form; + $this->assertEqual("Mark", $form->name); + } + + public function testPostMultidimensionalArray() + { + $response = Unirest::post("http://httpbin.org/post", array( + "Accept" => "application/json" + ), array( + 'key' => 'value', + 'items' => array( + 'item1', + 'item2' + ) + )); + + $this->assertEqual(200, $response->code); + + $form = $response->body->form; + $this->assertEqual("value", $form->key); + $this->assertEqual("item1", $form->{"items[0]"}); + $this->assertEqual("item2", $form->{"items[1]"}); + } + + public function testPut() + { + $response = Unirest::put("http://httpbin.org/put", array( + "Accept" => "application/json" + ), array( + "name" => "Mark", + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + + $form = $response->body->form; + $this->assertEqual("Mark", $form->name); + $this->assertEqual("thefosk", $form->nick); + } + + public function testPatch() + { + $response = Unirest::patch("http://httpbin.org/patch", array( + "Accept" => "application/json" + ), array( + "name" => "Mark", + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + + $form = $response->body->form; + $this->assertEqual("Mark", $form->name); + $this->assertEqual("thefosk", $form->nick); + } + + public function testDelete() + { + $response = Unirest::delete("http://httpbin.org/delete", array( + "Accept" => "application/json", + "Content-Type" => "application/x-www-form-urlencoded" + ), array( + "name" => "Mark", + "nick" => "thefosk" + )); + + $this->assertEqual(200, $response->code); + $data = $response->body->data; + $this->assertFalse(empty($data)); + } + + public function testTimeoutFail() + { + Unirest::timeout(1); + + $this->expectException(); + $response = Unirest::get("http://httpbin.org/delay/3"); + + Unirest::timeout(null); // Cleaning timeout for the other tests + } + + public function testTimeoutSuccess() + { + Unirest::timeout(3); + + $response = Unirest::get("http://httpbin.org/delay/1"); + $this->assertEqual(200, $response->code); + + Unirest::timeout(null); // Cleaning timeout for the other tests + } + + public function testDefaultHeader() + { + Unirest::defaultHeader("Hello", "custom"); + $response = Unirest::get("http://httpbin.org/get"); + + $this->assertEqual(200, $response->code); + $headers = $response->body->headers; + $properties = get_object_vars($headers); + $this->assertTrue(array_key_exists("Hello", $properties)); + $this->assertEqual("custom", $headers->Hello); + $response = Unirest::get("http://httpbin.org/get"); + + $this->assertEqual(200, $response->code); + $headers = $response->body->headers; + $properties = get_object_vars($headers); + $this->assertTrue(array_key_exists("Hello", $properties)); + $this->assertEqual("custom", $headers->Hello); + Unirest::clearDefaultHeaders(); + $response = Unirest::get("http://httpbin.org/get"); + + $this->assertEqual(200, $response->code); + $headers = $response->body->headers; + $properties = get_object_vars($headers); + $this->assertFalse(array_key_exists("Hello", $properties)); + } + + public function testGzip() + { + $response = Unirest::get("http://httpbin.org/gzip"); + $args = $response->body; + $this->assertEqual(true, $args->gzipped); + } + + public function testBasicAuthentication() + { + $response = Unirest::get("http://httpbin.org/get", null, null, "user", "password"); + $headers = $response->body->headers; + $this->assertEqual("Basic dXNlcjpwYXNzd29yZA==", $headers->Authorization); + } + } \ No newline at end of file From 6bb573b1f6a295e442fd78f57f3434aac485be37 Mon Sep 17 00:00:00 2001 From: thefosk Date: Tue, 18 Mar 2014 11:23:33 -0700 Subject: [PATCH 15/18] tests --- test/Unirest/UnirestTest.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index 3b3dd07..02f10d5 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -85,6 +85,23 @@ class UnirestTest extends UnitTestCase $this->assertEqual("Mark", $form->name); $this->assertEqual("thefosk", $form->nick); } + + public function testPostArray() + { + $response = Unirest::post("http://httpbin.org/post", array( + "Accept" => "application/json" + ), array( + "name[0]" => "Mark", + "name[1]" => "John" + )); + + $this->assertEqual(200, $response->code); + + $form = $response->body->form; + + $this->assertEqual("Mark", $form->{"name[0]"}); + $this->assertEqual("John", $form->{"name[1]"}); + } public function testPostWithDots() { @@ -117,10 +134,7 @@ class UnirestTest extends UnitTestCase } public function testUpload() - { - - var_dump(file_get_contents(dirname(__FILE__) . "/test_upload.txt")); - + { $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), array( @@ -130,8 +144,6 @@ class UnirestTest extends UnitTestCase $this->assertEqual(200, $response->code); $files = $response->body->files; - var_dump($response->body); - var_dump($files); $this->assertEqual("This is a test", $files->file); $form = $response->body->form; From a7797328fac021a7aebbd85488d3c4a03a1522c4 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 19 Mar 2014 12:18:52 -0700 Subject: [PATCH 16/18] closes #36 and #37 --- lib/Unirest/Unirest.php | 20 +++++++++++--------- test/Unirest/UnirestTest.php | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index 4f75700..86beea3 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -164,16 +164,20 @@ class Unirest { if ($headers == NULL) $headers = array(); + $lowercaseHeaders = array(); - foreach ($headers as $key => $val) { - $lowercaseHeaders[] = Unirest::getHeader($key, $val); - } - foreach (Unirest::$defaultHeaders as $key => $val) { + $finalHeaders = array_merge($headers, Unirest::$defaultHeaders); + foreach ($finalHeaders as $key => $val) { $lowercaseHeaders[] = Unirest::getHeader($key, $val); } - $lowercaseHeaders[] = "user-agent: unirest-php/1.1"; - $lowercaseHeaders[] = "expect:"; + $lowerCaseFinalHeaders = array_change_key_case($finalHeaders); + if (!array_key_exists("user-agent", $lowerCaseFinalHeaders)) { + $lowercaseHeaders[] = "user-agent: unirest-php/1.1"; + } + if (!array_key_exists("expect", $lowerCaseFinalHeaders)) { + $lowercaseHeaders[] = "expect:"; + } $ch = curl_init(); if ($httpMethod != HttpMethod::GET) { @@ -191,7 +195,7 @@ class Unirest $url .= "?"; } Unirest::http_build_query_for_curl($body, $postBody); - $url .= http_build_query($postBody); + $url .= urldecode(http_build_query($postBody)); } curl_setopt($ch, CURLOPT_URL, Unirest::encodeUrl($url)); @@ -267,8 +271,6 @@ class Unirest private static function getHeader($key, $val) { $key = trim(strtolower($key)); - if ($key == "user-agent" || $key == "expect") - continue; return $key . ": " . $val; } diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index 02f10d5..8dbd2f7 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -2,6 +2,7 @@ class UnirestTest extends UnitTestCase { + public function testGet() { $response = Unirest::get("http://httpbin.org/get?name=Mark", array( @@ -34,8 +35,8 @@ class UnirestTest extends UnitTestCase $args = $response->body->args; $this->assertEqual("value", $args->key); - $this->assertEqual("item1", $args->{"items%5B0%5D"}); - $this->assertEqual("item2", $args->{"items%5B1%5D"}); + $this->assertEqual("item1", $args->{"items[0]"}); + $this->assertEqual("item2", $args->{"items[1]"}); } public function testGetWithDots() @@ -66,7 +67,7 @@ class UnirestTest extends UnitTestCase $this->assertEqual(200, $response->code); $args = $response->body->args; - $this->assertEqual("Mark+Bond", $args->{"user.name"}); + $this->assertEqual("Mark Bond", $args->{"user.name"}); $this->assertEqual("thefosk", $args->nick); } @@ -102,6 +103,20 @@ class UnirestTest extends UnitTestCase $this->assertEqual("Mark", $form->{"name[0]"}); $this->assertEqual("John", $form->{"name[1]"}); } + + public function testGetArray() + { + $response = Unirest::get("http://httpbin.org/get", array(), array( + "name[0]" => "Mark", + "name[1]" => "John" + )); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + $this->assertEqual("Mark", $args->{"name[0]"}); + $this->assertEqual("John", $args->{"name[1]"}); + } public function testPostWithDots() { @@ -276,5 +291,17 @@ class UnirestTest extends UnitTestCase $headers = $response->body->headers; $this->assertEqual("Basic dXNlcjpwYXNzd29yZA==", $headers->Authorization); } + + public function testCustomHeaders() + { + $response = Unirest::get('http://httpbin.org/get', array( + 'user-agent' => 'ciao', + )); + + $this->assertEqual(200, $response->code); + + $headers = $response->body->headers; + $this->assertEqual("ciao", $headers->{'User-Agent'}); + } } \ No newline at end of file From 4d7aabfa47048d6cc25d8d50ab985d37c9319cfd Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 19 Mar 2014 12:23:55 -0700 Subject: [PATCH 17/18] closes #34 --- lib/Unirest/Unirest.php | 2 +- test/Unirest/UnirestTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index 86beea3..f190b25 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -234,7 +234,7 @@ class Unirest $pairs = explode("&", $querystring); $vars = array(); foreach ($pairs as $pair) { - $nv = explode("=", $pair); + $nv = explode("=", $pair, 2); $name = $nv[0]; $value = $nv[1]; $vars[$name] = $value; diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index 8dbd2f7..fc72659 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -87,6 +87,34 @@ class UnirestTest extends UnitTestCase $this->assertEqual("thefosk", $form->nick); } + public function testPostWithEqualSign() + { + $response = Unirest::post("http://httpbin.org/post", array( + "Accept" => "application/json" + ), array( + "name" => "Mark=Hello" + )); + + $this->assertEqual(200, $response->code); + + $form = $response->body->form; + $this->assertEqual("Mark=Hello", $form->name); + } + + public function testGetWithEqualSign() + { + $response = Unirest::get("http://httpbin.org/get", array( + "Accept" => "application/json" + ), array( + "name" => "Mark=Hello" + )); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + $this->assertEqual("Mark=Hello", $args->name); + } + public function testPostArray() { $response = Unirest::post("http://httpbin.org/post", array( From 099fbca53bfcf6deda9b4f4b837c13af543eaee3 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 19 Mar 2014 12:25:27 -0700 Subject: [PATCH 18/18] test --- test/Unirest/UnirestTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index fc72659..6157f20 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -113,6 +113,17 @@ class UnirestTest extends UnitTestCase $args = $response->body->args; $this->assertEqual("Mark=Hello", $args->name); + + $response = Unirest::get("http://httpbin.org/get", array( + "Accept" => "application/json" + ), array( + "name" => "Mark=Hello=John" + )); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + $this->assertEqual("Mark=Hello=John", $args->name); } public function testPostArray()