Merge pull request #121 from zachborboa/master

json-encode request when request header Content-Type is application/json and data is a multidimensional array
This commit is contained in:
Zach Borboa
2015-01-17 02:54:51 -08:00
3 changed files with 52 additions and 24 deletions
+1 -1
View File
@@ -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": "*"
+10 -2
View File
@@ -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) {
+41 -21
View File
@@ -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));
}
}
}
}