Allow the disabling of automatic response decoding.

The following will disable their respective decoders:

  $curl->setDefaultDecoder(false);
  $curl->setJsonDecoder(false);
  $curl->setXmlDecoder(false);
This commit is contained in:
Zach Borboa
2017-04-29 02:54:35 -07:00
parent 5706ad7723
commit efbcc080bb
6 changed files with 209 additions and 50 deletions
+6 -5
View File
@@ -141,6 +141,7 @@ echo $curl->responseHeaders['CoNTeNT-TyPE'] . "\n"; // image/png
```
```php
// Clean up.
$curl->close();
```
@@ -222,7 +223,7 @@ Curl::setCookieFile($cookie_file)
Curl::setCookieJar($cookie_jar)
Curl::setCookieString($string)
Curl::setCookies($cookies)
Curl::setDefaultDecoder($decoder = 'json')
Curl::setDefaultDecoder($mixed = 'json')
Curl::setDefaultJsonDecoder()
Curl::setDefaultTimeout()
Curl::setDefaultUserAgent()
@@ -230,7 +231,7 @@ Curl::setDefaultXmlDecoder()
Curl::setDigestAuthentication($username, $password = '')
Curl::setHeader($key, $value)
Curl::setHeaders($headers)
Curl::setJsonDecoder($function)
Curl::setJsonDecoder($mixed)
Curl::setMaxFilesize($bytes)
Curl::setOpt($option, $value)
Curl::setOpts($options)
@@ -240,7 +241,7 @@ Curl::setReferrer($referrer)
Curl::setTimeout($seconds)
Curl::setUrl($url, $mixed_data = '')
Curl::setUserAgent($user_agent)
Curl::setXmlDecoder($function)
Curl::setXmlDecoder($mixed)
Curl::success($callback)
Curl::unsetHeader($key)
Curl::verbose($on = true, $output = STDERR)
@@ -273,7 +274,7 @@ MultiCurl::setCookies($cookies)
MultiCurl::setDigestAuthentication($username, $password = '')
MultiCurl::setHeader($key, $value)
MultiCurl::setHeaders($headers)
MultiCurl::setJsonDecoder($function)
MultiCurl::setJsonDecoder($mixed)
MultiCurl::setOpt($option, $value)
MultiCurl::setOpts($options)
MultiCurl::setPort($port)
@@ -282,7 +283,7 @@ MultiCurl::setReferrer($referrer)
MultiCurl::setTimeout($seconds)
MultiCurl::setUrl($url)
MultiCurl::setUserAgent($user_agent)
MultiCurl::setXmlDecoder($function)
MultiCurl::setXmlDecoder($mixed)
MultiCurl::start()
MultiCurl::success($callback)
MultiCurl::unsetHeader($key)
+27 -23
View File
@@ -905,16 +905,18 @@ class Curl
* Set Default Decoder
*
* @access public
* @param $decoder string|callable
* @param $mixed boolean|callable|string
*/
public function setDefaultDecoder($decoder = 'json')
public function setDefaultDecoder($mixed = 'json')
{
if (is_callable($decoder)) {
$this->defaultDecoder = $decoder;
if ($mixed === false) {
$this->defaultDecoder = false;
} elseif (is_callable($mixed)) {
$this->defaultDecoder = $mixed;
} else {
if ($decoder === 'json') {
if ($mixed === 'json') {
$this->defaultDecoder = $this->jsonDecoder;
} elseif ($decoder === 'xml') {
} elseif ($mixed === 'xml') {
$this->defaultDecoder = $this->xmlDecoder;
}
}
@@ -988,13 +990,16 @@ class Curl
* Set JSON Decoder
*
* @access public
* @param $function
* @param $mixed boolean|callable
*/
public function setJsonDecoder($function)
public function setJsonDecoder($mixed)
{
if (is_callable($function)) {
$this->jsonDecoder = $function;
$this->jsonDecoderArgs = func_get_args();
if ($mixed === false) {
$this->jsonDecoder = false;
$this->jsonDecoderArgs = array();
} elseif (is_callable($mixed)) {
$this->jsonDecoder = $mixed;
$this->jsonDecoderArgs = array();
}
}
@@ -1002,12 +1007,14 @@ class Curl
* Set XML Decoder
*
* @access public
* @param $function
* @param $mixed boolean|callable
*/
public function setXmlDecoder($function)
public function setXmlDecoder($mixed)
{
if (is_callable($function)) {
$this->xmlDecoder = $function;
if ($mixed === false) {
$this->xmlDecoder = false;
} elseif (is_callable($mixed)) {
$this->xmlDecoder = $mixed;
}
}
@@ -1322,21 +1329,18 @@ class Curl
$response = $raw_response;
if (isset($response_headers['Content-Type'])) {
if (preg_match($this->jsonPattern, $response_headers['Content-Type'])) {
$json_decoder = $this->jsonDecoder;
if ($this->jsonDecoder) {
$args = $this->jsonDecoderArgs;
array_unshift($args, $response);
$response = call_user_func_array($json_decoder, $args);
$response = call_user_func_array($this->jsonDecoder, $args);
}
} elseif (preg_match($this->xmlPattern, $response_headers['Content-Type'])) {
$xml_decoder = $this->xmlDecoder;
if ($xml_decoder) {
$response = call_user_func($xml_decoder, $response);
if ($this->xmlDecoder) {
$response = call_user_func($this->xmlDecoder, $response);
}
} else {
$default_decoder = $this->defaultDecoder;
if ($default_decoder) {
$response = call_user_func($default_decoder, $response);
if ($this->defaultDecoder) {
$response = call_user_func($this->defaultDecoder, $response);
}
}
}
+12 -8
View File
@@ -514,12 +514,14 @@ class MultiCurl
* Set JSON Decoder
*
* @access public
* @param $function
* @param $mixed boolean|callable
*/
public function setJsonDecoder($function)
public function setJsonDecoder($mixed)
{
if (is_callable($function)) {
$this->jsonDecoder = $function;
if ($mixed === false) {
$this->jsonDecoder = false;
} elseif (is_callable($mixed)) {
$this->jsonDecoder = $mixed;
}
}
@@ -527,12 +529,14 @@ class MultiCurl
* Set XML Decoder
*
* @access public
* @param $function
* @param $mixed boolean|callable
*/
public function setXmlDecoder($function)
public function setXmlDecoder($mixed)
{
if (is_callable($function)) {
$this->xmlDecoder = $function;
if ($mixed === false) {
$this->xmlDecoder = false;
} elseif (is_callable($mixed)) {
$this->xmlDecoder = $mixed;
}
}
+61 -8
View File
@@ -1417,6 +1417,11 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$test->server('json_response', 'POST', $data);
$this->assertFalse(is_object($test->curl->response));
$this->assertTrue(is_array($test->curl->response));
$test = new Test();
$test->curl->setJsonDecoder(false);
$test->server('json_response', 'POST', $data);
$this->assertTrue(is_string($test->curl->response));
}
public function testJsonContentTypeDetection()
@@ -2588,7 +2593,7 @@ class CurlTest extends \PHPUnit\Framework\TestCase
'value' => $value,
));
$this->assertInstanceOf('SimpleXMLElement', $test->curl->response);
$this->assertInstanceOf(SimpleXMLElement::class, $test->curl->response);
$doc = new DOMDocument();
$doc->formatOutput = true;
@@ -2947,21 +2952,69 @@ class CurlTest extends \PHPUnit\Framework\TestCase
public function testXMLDecoder()
{
$data = array(
'key' => 'Content-Type',
'value' => 'text/xml',
);
$test = new Test();
$test->server('xml_with_cdata_response', 'POST', $data);
$test->server('xml_with_cdata_response', 'POST');
$this->assertTrue(is_object($test->curl->response));
$this->assertInstanceOf(SimpleXMLElement::class, $test->curl->response);
$this->assertFalse(strpos($test->curl->response->saveXML(), '<![CDATA[') === false);
$test = new Test();
$test->curl->setXmlDecoder(function ($response) {
return simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
});
$test->server('xml_with_cdata_response', 'POST', $data);
$test->server('xml_with_cdata_response', 'POST');
$this->assertTrue(is_object($test->curl->response));
$this->assertInstanceOf(SimpleXMLElement::class, $test->curl->response);
$this->assertTrue(strpos($test->curl->response->saveXML(), '<![CDATA[') === false);
$test = new Test();
$test->curl->setXmlDecoder(false);
$test->server('xml_with_cdata_response', 'POST');
$this->assertTrue(is_string($test->curl->response));
}
public function testDefaultDecoder()
{
// Default.
$test = new Test();
$test->server('download_file_size', 'GET');
$this->assertTrue(is_string($test->curl->response));
// Callable.
$test = new Test();
$test->curl->setDefaultDecoder(function ($response) {
return '123';
});
$test->server('download_file_size', 'GET');
$this->assertEquals('123', $test->curl->response);
// "json".
$test = new Test();
$test->curl->setDefaultDecoder('json');
$test->server('json_response', 'POST', array(
'key' => 'Content-Type',
'value' => 'application/but-not-json',
));
$this->assertInstanceOf(stdClass::class, $test->curl->response);
// "xml".
$test = new Test();
$test->curl->setDefaultDecoder('xml');
$test->server('xml_response', 'POST', array(
'key' => 'Content-Type',
'value' => 'text/but-not-xml',
));
$this->assertInstanceOf(SimpleXMLElement::class, $test->curl->response);
// False.
$test = new Test();
$test->curl->setDefaultDecoder('json');
$test->curl->setDefaultDecoder(false);
$test->server('json_response', 'POST', array(
'key' => 'Content-Type',
'value' => 'application/but-not-json',
));
$this->assertTrue(is_string($test->curl->response));
}
public function testTotalTime()
+102 -5
View File
@@ -2079,8 +2079,40 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('yummy', $get_2->responseCookies['mycookie']);
}
public function testJSONDecoder()
public function testJsonDecoder()
{
$data = array(
'key' => 'Content-Type',
'value' => 'application/json',
);
$multi_curl = new MultiCurl();
$multi_curl->setHeader('X-DEBUG-TEST', 'json_response');
$post_1 = $multi_curl->addPost(Test::TEST_URL, $data);
$post_1->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertTrue(is_object($instance->response));
\PHPUnit\Framework\Assert::assertFalse(is_array($instance->response));
});
$post_2 = $multi_curl->addPost(Test::TEST_URL, $data);
$post_2->setJsonDecoder(function ($response) {
return json_decode($response, true);
});
$post_2->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertFalse(is_object($instance->response));
\PHPUnit\Framework\Assert::assertTrue(is_array($instance->response));
});
$post_3 = $multi_curl->addPost(Test::TEST_URL, $data);
$post_3->setJsonDecoder(false);
$post_3->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertTrue(is_string($instance->response));
});
$multi_curl->start();
$multi_curl = new MultiCurl();
$multi_curl->setHeader('X-DEBUG-TEST', 'json_response');
$multi_curl->setJsonDecoder(function ($response) {
@@ -2089,7 +2121,6 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$get_1 = $multi_curl->addGet(Test::TEST_URL);
$get_1->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertInstanceOf('Curl\Curl', $instance);
\PHPUnit\Framework\Assert::assertEquals('foo', $instance->response);
});
@@ -2100,17 +2131,65 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
});
});
$get_2->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertInstanceOf('Curl\Curl', $instance);
\PHPUnit\Framework\Assert::assertEquals('bar', $instance->response);
});
$get_3 = $multi_curl->addGet(Test::TEST_URL);
$get_3->beforeSend(function ($instance) {
$instance->setJsonDecoder(false);
});
$get_3->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertTrue(is_string($instance->response));
});
$multi_curl->start();
$this->assertEquals('foo', $get_1->response);
$this->assertEquals('bar', $get_2->response);
$multi_curl = new MultiCurl();
$multi_curl->setHeader('X-DEBUG-TEST', 'json_response');
$multi_curl->setJsonDecoder(false);
$get_4 = $multi_curl->addGet(Test::TEST_URL);
$get_4->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertTrue(is_string($instance->response));
});
$multi_curl->start();
}
public function testXMLDecoder()
{
$multi_curl = new MultiCurl();
$multi_curl->setHeader('X-DEBUG-TEST', 'xml_with_cdata_response');
$post_1 = $multi_curl->addPost(Test::TEST_URL);
$post_1->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertTrue(is_object($instance->response));
\PHPUnit\Framework\Assert::assertInstanceOf(SimpleXMLElement::class, $instance->response);
\PHPUnit\Framework\Assert::assertFalse(strpos($instance->response->saveXML(), '<![CDATA[') === false);
});
$post_2 = $multi_curl->addPost(Test::TEST_URL);
$post_2->setXmlDecoder(function ($response) {
return simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
});
$post_2->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertTrue(is_object($instance->response));
\PHPUnit\Framework\Assert::assertInstanceOf(SimpleXMLElement::class, $instance->response);
\PHPUnit\Framework\Assert::assertTrue(strpos($instance->response->saveXML(), '<![CDATA[') === false);
});
$post_3 = $multi_curl->addPost(Test::TEST_URL);
$post_3->setXmlDecoder(false);
$post_3->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertTrue(is_string($instance->response));
});
$multi_curl->start();
$multi_curl = new MultiCurl();
$multi_curl->setHeader('X-DEBUG-TEST', 'xml_with_cdata_response');
$multi_curl->setXmlDecoder(function ($response) {
@@ -2119,7 +2198,6 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$get_1 = $multi_curl->addGet(Test::TEST_URL);
$get_1->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertInstanceOf('Curl\Curl', $instance);
\PHPUnit\Framework\Assert::assertEquals('foo', $instance->response);
});
@@ -2130,13 +2208,32 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
});
});
$get_2->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertInstanceOf('Curl\Curl', $instance);
\PHPUnit\Framework\Assert::assertEquals('bar', $instance->response);
});
$get_3 = $multi_curl->addGet(Test::TEST_URL);
$get_3->beforeSend(function ($instance) {
$instance->setXmlDecoder(false);
});
$get_3->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertTrue(is_string($instance->response));
});
$multi_curl->start();
$this->assertEquals('foo', $get_1->response);
$this->assertEquals('bar', $get_2->response);
$multi_curl = new MultiCurl();
$multi_curl->setHeader('X-DEBUG-TEST', 'xml_with_cdata_response');
$multi_curl->setXmlDecoder(false);
$get_4 = $multi_curl->addGet(Test::TEST_URL);
$get_4->complete(function ($instance) {
\PHPUnit\Framework\Assert::assertTrue(is_string($instance->response));
});
$multi_curl->start();
}
public function testDownloadCallback()
+1 -1
View File
@@ -253,7 +253,7 @@ if ($test === 'http_basic_auth') {
readfile($unsafe_file_path);
exit;
} elseif ($test === 'download_file_size') {
$bytes = $_GET['bytes'];
$bytes = isset($_GET['bytes']) ? $_GET['bytes'] : 1234;
$str = str_repeat('.', $bytes);
header('Content-Type: application/octet-stream');
header('Content-Length: ' . strlen($str));