diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index b962716..c159dae 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -134,6 +134,7 @@ class Curl * @param $data * * @return array|string + * @throws \ErrorException */ public function buildPostData($data) { @@ -142,9 +143,9 @@ class Curl // Return JSON-encoded string when the request's content-type is JSON. if (isset($this->headers['Content-Type']) && preg_match($this->jsonPattern, $this->headers['Content-Type'])) { - $json_str = json_encode($data); - if (!($json_str === false)) { - $data = $json_str; + $data = json_encode($data); + if (!(json_last_error() === JSON_ERROR_NONE)) { + throw new \ErrorException('json_encode error: ' . json_last_error_msg()); } } else { // Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch, diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 6266b42..8737c17 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -1381,6 +1381,20 @@ class CurlTest extends \PHPUnit\Framework\TestCase } } + /** + * @expectedException \ErrorException + */ + public function testJsonEncode() + { + $data = array( + 'malformed' => pack('H*', 'c32e'), + ); + + $test = new Test(); + $test->curl->setHeader('Content-Type', 'application/json'); + $test->server('post_json', 'POST', $data); + } + public function testJsonDecoderOptions() { // Implicit default json decoder should return object.