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(