mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-31 04:34:27 +00:00
Throw error when json_encode fails
This commit is contained in:
+4
-3
@@ -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,
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user