Skip to content

Commit 27373e7

Browse files
committed
Merge pull request php-curl-class#128 from zachborboa/master
Fix php-curl-class#126: Add configurable json decoder
2 parents 9adaf86 + 85cd7e4 commit 27373e7

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/Curl/Curl.php

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

20+
private $json_decoder = null;
2021
private $json_pattern = '~^application/(?:json|vnd\.api\+json)~i';
2122
private $xml_pattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i';
2223

@@ -50,6 +51,7 @@ public function __construct()
5051

5152
$this->curl = curl_init();
5253
$this->setDefaultUserAgent();
54+
$this->setDefaultJsonDecoder();
5355
$this->setOpt(CURLINFO_HEADER_OUT, true);
5456
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
5557
$this->headers = new CaseInsensitiveArray();
@@ -241,6 +243,20 @@ public function setDefaultUserAgent()
241243
$this->setUserAgent($user_agent);
242244
}
243245

246+
public function setDefaultJsonDecoder() {
247+
$this->json_decoder = function($response) {
248+
$json_obj = json_decode($response, false);
249+
if (!($json_obj === null)) {
250+
$response = $json_obj;
251+
}
252+
return $response;
253+
};
254+
}
255+
256+
public function setJsonDecoder($func) {
257+
$this->json_decoder = $func;
258+
}
259+
244260
public function setUserAgent($user_agent)
245261
{
246262
$this->setOpt(CURLOPT_USERAGENT, $user_agent);
@@ -370,14 +386,11 @@ private function parseRequestHeaders($raw_headers)
370386

371387
private function parseResponse($response_headers, $raw_response)
372388
{
373-
374389
$response = $raw_response;
375390
if (isset($response_headers['Content-Type'])) {
376391
if (preg_match($this->json_pattern, $response_headers['Content-Type'])) {
377-
$json_obj = json_decode($response, false);
378-
if ($json_obj !== null) {
379-
$response = $json_obj;
380-
}
392+
$json_decoder = $this->json_decoder;
393+
$response = $json_decoder($response);
381394
} elseif (preg_match($this->xml_pattern, $response_headers['Content-Type'])) {
382395
$xml_obj = @simplexml_load_string($response);
383396
if (!($xml_obj === false)) {

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,27 @@ public function testJSONResponse()
735735
}
736736
}
737737

738+
public function testJSONDecoder()
739+
{
740+
$data = array(
741+
'key' => 'Content-Type',
742+
'value' => 'application/json',
743+
);
744+
745+
$test = new Test();
746+
$test->server('json_response', 'POST', $data);
747+
$this->assertTrue(is_object($test->curl->response));
748+
$this->assertFalse(is_array($test->curl->response));
749+
750+
$test = new Test();
751+
$test->curl->setJsonDecoder(function($response) {
752+
return json_decode($response, true);
753+
});
754+
$test->server('json_response', 'POST', $data);
755+
$this->assertFalse(is_object($test->curl->response));
756+
$this->assertTrue(is_array($test->curl->response));
757+
}
758+
738759
public function testXMLResponse()
739760
{
740761
foreach (array(

0 commit comments

Comments
 (0)