From bd4975cb3c5e64093130ec20608d4c4da52a32a3 Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 8 Jan 2014 18:14:42 -0800 Subject: [PATCH] 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