Skip to content

Commit e4d7908

Browse files
authored
Merge pull request php-curl-class#544 from zachborboa/master
Fix xml response when xml decode fails
2 parents 34c751c + fc54b09 commit e4d7908

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

src/Curl/Curl.php

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ interface_exists('JsonSerializable', false) &&
151151
$data instanceof \JsonSerializable
152152
)
153153
)) {
154-
$data = \Curl\json_encode($data);
154+
$data = \Curl\Encoder::encodeJson($data);
155155
} elseif (is_array($data)) {
156156
// Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch,
157157
// CURLOPT_POSTFIELDS, $data) doesn't correctly handle multi-dimensional arrays when files are
@@ -1760,34 +1760,3 @@ function createHeaderCallback($header_callback_data) {
17601760
return strlen($header);
17611761
};
17621762
}
1763-
1764-
/**
1765-
* Json Encode
1766-
*
1767-
* Wrap json_encode() to throw error when the value being encoded fails.
1768-
*
1769-
* @param $value
1770-
* @param $options
1771-
* @param $depth
1772-
*
1773-
* @return string
1774-
* @throws \ErrorException
1775-
*/
1776-
function json_encode($value, $options = 0, $depth = 512) {
1777-
// Make compatible with PHP version both before and after 5.5.0. PHP 5.5.0 added the $depth parameter.
1778-
$gte_v550 = version_compare(PHP_VERSION, '5.5.0') >= 0;
1779-
if ($gte_v550) {
1780-
$value = \json_encode($value, $options, $depth);
1781-
} else {
1782-
$value = \json_encode($value, $options);
1783-
}
1784-
if (!(json_last_error() === JSON_ERROR_NONE)) {
1785-
if (function_exists('json_last_error_msg')) {
1786-
$error_message = 'json_encode error: ' . json_last_error_msg();
1787-
} else {
1788-
$error_message = 'json_encode error';
1789-
}
1790-
throw new \ErrorException($error_message);
1791-
}
1792-
return $value;
1793-
}

src/Curl/Decoder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public static function decodeJson()
4444
public static function decodeXml()
4545
{
4646
$args = func_get_args();
47-
$xml_obj = @call_user_func_array('simplexml_load_string', $args);
48-
if (!($xml_obj === false)) {
49-
$response = $xml_obj;
47+
$response = @call_user_func_array('simplexml_load_string', $args);
48+
if ($response === false) {
49+
$response = $args['0'];
5050
}
5151
return $response;
5252
}

src/Curl/Encoder.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Curl;
4+
5+
class Encoder
6+
{
7+
/**
8+
* Encode JSON
9+
*
10+
* Wrap json_encode() to throw error when the value being encoded fails.
11+
*
12+
* @access public
13+
* @param $value
14+
* @param $options
15+
* @param $depth
16+
*
17+
* @return string
18+
* @throws \ErrorException
19+
*/
20+
public static function encodeJson()
21+
{
22+
$args = func_get_args();
23+
24+
// Call json_encode() without the $depth parameter in PHP
25+
// versions less than 5.5.0 as the $depth parameter was added in
26+
// PHP version 5.5.0.
27+
if (version_compare(PHP_VERSION, '5.5.0', '<')) {
28+
$args = array_slice($args, 0, 2);
29+
}
30+
31+
$value = call_user_func_array('json_encode', $args);
32+
if (!(json_last_error() === JSON_ERROR_NONE)) {
33+
if (function_exists('json_last_error_msg')) {
34+
$error_message = 'json_encode error: ' . json_last_error_msg();
35+
} else {
36+
$error_message = 'json_encode error';
37+
}
38+
throw new \ErrorException($error_message);
39+
}
40+
return $value;
41+
}
42+
}

0 commit comments

Comments
 (0)