Skip to content

Commit 4fab0f3

Browse files
committed
Merge pull request php-curl-class#121 from zachborboa/master
json-encode request when request header Content-Type is application/json and data is a multidimensional array
2 parents f6b626d + 1243678 commit 4fab0f3

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"homepage": "https://github.com/php-curl-class/php-curl-class",
55
"license": "Unlicense",
66
"keywords": ["php", "curl", "class"],
7-
"version": "2.1.2",
7+
"version": "2.1.3",
88
"require": {
99
"php": ">=5.3",
1010
"ext-curl": "*"

src/Curl/Curl.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Curl
66
{
7-
const VERSION = '2.1.2';
7+
const VERSION = '2.1.3';
88

99
private $cookies = array();
1010
private $headers = array();
@@ -413,7 +413,15 @@ private function postfields($data)
413413
{
414414
if (is_array($data)) {
415415
if (self::is_array_multidim($data)) {
416-
$data = self::http_build_multi_query($data);
416+
if (isset($this->headers['Content-Type']) &&
417+
preg_match($this->json_pattern, $this->headers['Content-Type'])) {
418+
$json_str = json_encode($data);
419+
if (!($json_str === false)) {
420+
$data = $json_str;
421+
}
422+
} else {
423+
$data = self::http_build_multi_query($data);
424+
}
417425
} else {
418426
$binary_data = false;
419427
foreach ($data as $key => $value) {

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -644,31 +644,51 @@ public function testPostCurlFileFormDataContentType()
644644

645645
public function testJSONRequest()
646646
{
647-
$data = array('key' => 'value');
648-
$expected_response = '{"key":"value"}';
647+
foreach (
648+
array(
649+
array(
650+
array(
651+
'key' => 'value',
652+
),
653+
'{"key":"value"}',
654+
),
655+
array(
656+
array(
657+
'key' => 'value',
658+
'strings' => array(
659+
'a',
660+
'b',
661+
'c',
662+
),
663+
),
664+
'{"key":"value","strings":["a","b","c"]}',
665+
),
666+
) as $test) {
667+
list($data, $expected_response) = $test;
649668

650-
$test = new Test();
651-
$this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data)));
669+
$test = new Test();
670+
$this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data)));
652671

653-
foreach (array(
654-
'Content-Type',
655-
'content-type',
656-
'CONTENT-TYPE') as $key) {
657672
foreach (array(
658-
'APPLICATION/JSON',
659-
'APPLICATION/JSON; CHARSET=UTF-8',
660-
'APPLICATION/JSON;CHARSET=UTF-8',
661-
'application/json',
662-
'application/json; charset=utf-8',
663-
'application/json;charset=UTF-8',
664-
) as $value) {
665-
$test = new Test();
666-
$test->curl->setHeader($key, $value);
667-
$this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data)));
668-
669-
$test = new Test();
670-
$test->curl->setHeader($key, $value);
671-
$this->assertEquals($expected_response, $test->server('post_json', 'POST', $data));
673+
'Content-Type',
674+
'content-type',
675+
'CONTENT-TYPE') as $key) {
676+
foreach (array(
677+
'APPLICATION/JSON',
678+
'APPLICATION/JSON; CHARSET=UTF-8',
679+
'APPLICATION/JSON;CHARSET=UTF-8',
680+
'application/json',
681+
'application/json; charset=utf-8',
682+
'application/json;charset=UTF-8',
683+
) as $value) {
684+
$test = new Test();
685+
$test->curl->setHeader($key, $value);
686+
$this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data)));
687+
688+
$test = new Test();
689+
$test->curl->setHeader($key, $value);
690+
$this->assertEquals($expected_response, $test->server('post_json', 'POST', $data));
691+
}
672692
}
673693
}
674694
}

0 commit comments

Comments
 (0)