diff --git a/composer.json b/composer.json index 5d2df54..b4bc34b 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "homepage": "https://github.com/php-curl-class/php-curl-class", "license": "Unlicense", "keywords": ["php", "curl", "class"], - "version": "2.1.2", + "version": "2.1.3", "require": { "php": ">=5.3", "ext-curl": "*" diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 2659b26..e85bc09 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -4,7 +4,7 @@ namespace Curl; class Curl { - const VERSION = '2.1.2'; + const VERSION = '2.1.3'; private $cookies = array(); private $headers = array(); @@ -413,7 +413,15 @@ class Curl { if (is_array($data)) { if (self::is_array_multidim($data)) { - $data = self::http_build_multi_query($data); + if (isset($this->headers['Content-Type']) && + preg_match($this->json_pattern, $this->headers['Content-Type'])) { + $json_str = json_encode($data); + if (!($json_str === false)) { + $data = $json_str; + } + } else { + $data = self::http_build_multi_query($data); + } } else { $binary_data = false; foreach ($data as $key => $value) { diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 39f8937..51090f7 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -644,31 +644,51 @@ class CurlTest extends PHPUnit_Framework_TestCase public function testJSONRequest() { - $data = array('key' => 'value'); - $expected_response = '{"key":"value"}'; + foreach ( + array( + array( + array( + 'key' => 'value', + ), + '{"key":"value"}', + ), + array( + array( + 'key' => 'value', + 'strings' => array( + 'a', + 'b', + 'c', + ), + ), + '{"key":"value","strings":["a","b","c"]}', + ), + ) as $test) { + list($data, $expected_response) = $test; - $test = new Test(); - $this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data))); + $test = new Test(); + $this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data))); - foreach (array( - 'Content-Type', - 'content-type', - 'CONTENT-TYPE') as $key) { foreach (array( - 'APPLICATION/JSON', - 'APPLICATION/JSON; CHARSET=UTF-8', - 'APPLICATION/JSON;CHARSET=UTF-8', - 'application/json', - 'application/json; charset=utf-8', - 'application/json;charset=UTF-8', - ) as $value) { - $test = new Test(); - $test->curl->setHeader($key, $value); - $this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data))); + 'Content-Type', + 'content-type', + 'CONTENT-TYPE') as $key) { + foreach (array( + 'APPLICATION/JSON', + 'APPLICATION/JSON; CHARSET=UTF-8', + 'APPLICATION/JSON;CHARSET=UTF-8', + 'application/json', + 'application/json; charset=utf-8', + 'application/json;charset=UTF-8', + ) as $value) { + $test = new Test(); + $test->curl->setHeader($key, $value); + $this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data))); - $test = new Test(); - $test->curl->setHeader($key, $value); - $this->assertEquals($expected_response, $test->server('post_json', 'POST', $data)); + $test = new Test(); + $test->curl->setHeader($key, $value); + $this->assertEquals($expected_response, $test->server('post_json', 'POST', $data)); + } } } }