Skip to content

Commit 80adf0f

Browse files
committed
Add json encoding when request header Content-Type is set to application/json
1 parent 5d7d25a commit 80adf0f

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

src/Curl/Curl.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class Curl
1717
private $error_function = null;
1818
private $complete_function = null;
1919

20+
private $json_pattern = '~^application/(?:json|vnd\.api\+json)~i';
21+
private $xml_pattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i';
22+
2023
public $curl;
2124
public $curls;
2225

@@ -369,12 +372,12 @@ private function parseResponse($response_headers, $raw_response)
369372

370373
$response = $raw_response;
371374
if (isset($response_headers['Content-Type'])) {
372-
if (preg_match('~^application/(?:json|vnd\.api\+json)~i', $response_headers['Content-Type'])) {
375+
if (preg_match($this->json_pattern, $response_headers['Content-Type'])) {
373376
$json_obj = json_decode($response, false);
374377
if ($json_obj !== null) {
375378
$response = $json_obj;
376379
}
377-
} elseif (preg_match('~^(?:text/|application/(?:atom\+|rss\+)?)xml~i', $response_headers['Content-Type'])) {
380+
} elseif (preg_match($this->xml_pattern, $response_headers['Content-Type'])) {
378381
$xml_obj = @simplexml_load_string($response);
379382
if (!($xml_obj === false)) {
380383
$response = $xml_obj;
@@ -432,7 +435,15 @@ private function postfields($data)
432435
}
433436

434437
if (!$binary_data) {
435-
$data = http_build_query($data);
438+
if (isset($this->headers['Content-Type']) &&
439+
preg_match($this->json_pattern, $this->headers['Content-Type'])) {
440+
$json_str = json_encode($data);
441+
if (!($json_str === false)) {
442+
$data = $json_str;
443+
}
444+
} else {
445+
$data = http_build_query($data);
446+
}
436447
}
437448
}
438449
}

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,37 @@ public function testPostCurlFileFormDataContentType()
649649
$this->assertFalse(file_exists($file_path));
650650
}
651651

652+
public function testJSONRequest()
653+
{
654+
$data = array('key' => 'value');
655+
$expected_response = '{"key":"value"}';
656+
657+
$test = new Test();
658+
$this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data)));
659+
660+
foreach (array(
661+
'Content-Type',
662+
'content-type',
663+
'CONTENT-TYPE') as $key) {
664+
foreach (array(
665+
'APPLICATION/JSON',
666+
'APPLICATION/JSON; CHARSET=UTF-8',
667+
'APPLICATION/JSON;CHARSET=UTF-8',
668+
'application/json',
669+
'application/json; charset=utf-8',
670+
'application/json;charset=UTF-8',
671+
) as $value) {
672+
$test = new Test();
673+
$test->curl->setHeader($key, $value);
674+
$this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data)));
675+
676+
$test = new Test();
677+
$test->curl->setHeader($key, $value);
678+
$this->assertEquals($expected_response, $test->server('post_json', 'POST', $data));
679+
}
680+
}
681+
}
682+
652683
public function testJSONResponse()
653684
{
654685
function assertion($key, $value)

tests/PHPCurlClass/server.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
} elseif ($test === 'post') {
5151
echo http_build_query($_POST);
5252
exit;
53+
} elseif ($test === 'post_json') {
54+
echo $http_raw_post_data;
55+
exit;
5356
} elseif ($test === 'put') {
5457
echo $http_raw_post_data;
5558
exit;

0 commit comments

Comments
 (0)