mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-09-13 03:46:32 +00:00
Fix #28: Decode json responses seamlessly
This commit is contained in:
@@ -275,6 +275,15 @@ class Curl {
|
||||
list($response_header, $ch->response) = explode("\r\n\r\n", $ch->response, 2);
|
||||
}
|
||||
$ch->response_headers = $this->_parseHeaders($response_header);
|
||||
|
||||
if (isset($ch->response_headers['Content-Type'])) {
|
||||
if (preg_match('/^application\/json/i', $ch->response_headers['Content-Type'])) {
|
||||
$json_obj = json_decode($ch->response, false);
|
||||
if (!is_null($json_obj)) {
|
||||
$ch->response = $json_obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ch->http_error_message = $ch->error ? (isset($ch->response_headers['0']) ? $ch->response_headers['0'] : '') : '';
|
||||
|
||||
@@ -203,7 +203,7 @@ class CurlTest extends PHPUnit_Framework_TestCase {
|
||||
$test = new Test();
|
||||
$test->curl->setBasicAuthentication($username, $password);
|
||||
$test->server('http_basic_auth', 'GET');
|
||||
$json = json_decode($test->curl->response);
|
||||
$json = $test->curl->response;
|
||||
$this->assertTrue($json->username === $username);
|
||||
$this->assertTrue($json->password === $password);
|
||||
}
|
||||
@@ -352,6 +352,33 @@ class CurlTest extends PHPUnit_Framework_TestCase {
|
||||
$this->assertTrue(!empty($content_type));
|
||||
}
|
||||
|
||||
public function testJSONResponse() {
|
||||
function assertion($key, $value) {
|
||||
$test = new Test();
|
||||
$test->server('json_response', 'POST', array(
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
));
|
||||
|
||||
$response = $test->curl->response;
|
||||
PHPUnit_Framework_Assert::assertNotNull($response);
|
||||
PHPUnit_Framework_Assert::assertNull($response->null);
|
||||
PHPUnit_Framework_Assert::assertTrue($response->true);
|
||||
PHPUnit_Framework_Assert::assertFalse($response->false);
|
||||
PHPUnit_Framework_Assert::assertTrue(is_int($response->integer));
|
||||
PHPUnit_Framework_Assert::assertTrue(is_float($response->float));
|
||||
PHPUnit_Framework_Assert::assertEmpty($response->empty);
|
||||
PHPUnit_Framework_Assert::assertTrue(is_string($response->string));
|
||||
}
|
||||
|
||||
assertion('Content-Type', 'application/json; charset=utf-8');
|
||||
assertion('content-type', 'application/json; charset=utf-8');
|
||||
assertion('Content-Type', 'application/json');
|
||||
assertion('content-type', 'application/json');
|
||||
assertion('CONTENT-TYPE', 'application/json');
|
||||
assertion('CONTENT-TYPE', 'APPLICATION/JSON');
|
||||
}
|
||||
|
||||
public function testArrayToStringConversion() {
|
||||
$test = new Test();
|
||||
$test->server('post', 'POST', array(
|
||||
|
||||
@@ -84,6 +84,21 @@ else if ($test === 'response_header') {
|
||||
header('ETag: ' . md5('worldpeace'));
|
||||
exit;
|
||||
}
|
||||
else if ($test === 'json_response') {
|
||||
$key = $_POST['key'];
|
||||
$value = $_POST['value'];
|
||||
header($key . ': ' . $value);
|
||||
echo json_encode(array(
|
||||
'null' => null,
|
||||
'true' => true,
|
||||
'false' => false,
|
||||
'integer' => 1,
|
||||
'float' => 3.14,
|
||||
'empty' => '',
|
||||
'string' => 'string',
|
||||
));
|
||||
exit;
|
||||
}
|
||||
|
||||
header('Content-Type: text/plain');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user