mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-09-03 22:36:27 +00:00
Fix #357: Allow passing json_decode() parameters to Curl::setDefaultJsonDecoder
This commit is contained in:
@@ -53,28 +53,8 @@ $curl->post('http://www.example.com/login/', array(
|
||||
'username' => 'myusername',
|
||||
'password' => 'mypassword',
|
||||
));
|
||||
|
||||
// Perform a post-redirect-get request (POST data and follow 303 redirections using GET requests).
|
||||
$curl = new Curl();
|
||||
$curl->setOpt(CURLOPT_FOLLOWLOCATION, true);¬
|
||||
$curl->post('http://www.example.com/login/', array(
|
||||
'username' => 'myusername',
|
||||
'password' => 'mypassword',
|
||||
));
|
||||
|
||||
// POST data and follow 303 redirections by POSTing data again.
|
||||
// Please note that 303 redirections should not be handled this way:
|
||||
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4
|
||||
$curl = new Curl();
|
||||
$curl->setOpt(CURLOPT_FOLLOWLOCATION, true);¬
|
||||
$curl->post('http://www.example.com/login/', array(
|
||||
'username' => 'myusername',
|
||||
'password' => 'mypassword',
|
||||
), false);
|
||||
```
|
||||
|
||||
A POST request performs by default a post-redirect-get (see above). Other request methods force an option which conflicts with the post-redirect-get behavior. Due to technical limitations of PHP engines <5.5.11 and HHVM, it is not possible to reset this option. It is therefore impossible to perform a post-redirect-get request using a php-curl-class Curl object that has already been used to perform other types of requests. Either use a new php-curl-class Curl object or upgrade your PHP engine.
|
||||
|
||||
```php
|
||||
$curl = new Curl();
|
||||
$curl->setBasicAuthentication('username', 'password');
|
||||
@@ -223,6 +203,7 @@ Curl::setCookie($key, $value)
|
||||
Curl::setCookieFile($cookie_file)
|
||||
Curl::setCookieJar($cookie_jar)
|
||||
Curl::setCookieString($string)
|
||||
Curl::setDefaultDecoder($decoder = 'json')
|
||||
Curl::setDefaultJsonDecoder()
|
||||
Curl::setDefaultTimeout()
|
||||
Curl::setDefaultUserAgent()
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
// Perform a post-redirect-get request (POST data and follow 303 redirections
|
||||
// using GET requests).
|
||||
$curl = new Curl();
|
||||
$curl->setOpt(CURLOPT_FOLLOWLOCATION, true);
|
||||
$curl->post('http://www.example.com/login/', array(
|
||||
'username' => 'myusername',
|
||||
'password' => 'mypassword',
|
||||
));
|
||||
|
||||
// POST data and follow 303 redirections by POSTing data again. Please note
|
||||
// that 303 redirections should not be handled this way.
|
||||
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4
|
||||
$curl = new Curl();
|
||||
$curl->setOpt(CURLOPT_FOLLOWLOCATION, true);
|
||||
$curl->post('http://www.example.com/login/', array(
|
||||
'username' => 'myusername',
|
||||
'password' => 'mypassword',
|
||||
), false);
|
||||
|
||||
// A POST request performs a post-redirect-get by default. Other request
|
||||
// methods force an option which conflicts with the post-redirect-get behavior.
|
||||
// Due to technical limitations of PHP engines <5.5.11 and HHVM, it is not
|
||||
// possible to reset this option. It is therefore impossible to perform a
|
||||
// post-redirect-get request using a php-curl-class Curl object that has already
|
||||
// been used to perform other types of requests. Either use a new php-curl-class
|
||||
// Curl object or upgrade your PHP engine.
|
||||
+15
-2
@@ -750,11 +750,24 @@ class Curl
|
||||
* Set Default JSON Decoder
|
||||
*
|
||||
* @access public
|
||||
* @param $assoc
|
||||
* @param $depth
|
||||
* @param $options
|
||||
*/
|
||||
public function setDefaultJsonDecoder()
|
||||
{
|
||||
$this->jsonDecoder = function($response) {
|
||||
$json_obj = json_decode($response, false);
|
||||
$args = func_get_args();
|
||||
$this->jsonDecoder = function($response) use ($args) {
|
||||
array_unshift($args, $response);
|
||||
|
||||
// Call json_decode() without the $options parameter in PHP
|
||||
// versions less than 5.4.0 as the $options parameter was added in
|
||||
// PHP version 5.4.0.
|
||||
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
|
||||
$args = array_slice($args, 0, 3);
|
||||
}
|
||||
|
||||
$json_obj = call_user_func_array('json_decode', $args);
|
||||
if (!($json_obj === null)) {
|
||||
$response = $json_obj;
|
||||
}
|
||||
|
||||
@@ -1040,7 +1040,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertFalse(file_exists($file_path));
|
||||
}
|
||||
|
||||
public function testJSONRequest()
|
||||
public function testJsonRequest()
|
||||
{
|
||||
foreach (
|
||||
array(
|
||||
@@ -1091,7 +1091,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testJSONResponse()
|
||||
public function testJsonResponse()
|
||||
{
|
||||
foreach (array(
|
||||
'Content-Type',
|
||||
@@ -1133,7 +1133,30 @@ class CurlTest extends PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testJSONDecoder()
|
||||
public function testJsonDecoderOptions()
|
||||
{
|
||||
// Implicit default json decoder should return object.
|
||||
$test = new Test();
|
||||
$test->server('json_response', 'GET');
|
||||
$this->assertTrue(is_object($test->curl->response));
|
||||
|
||||
// Explicit default json decoder should return object.
|
||||
$test = new Test();
|
||||
$test->curl->setDefaultJsonDecoder();
|
||||
$test->server('json_response', 'GET');
|
||||
$this->assertTrue(is_object($test->curl->response));
|
||||
|
||||
// Explicit default json decoder with options should return associative array as specified.
|
||||
$assoc = true;
|
||||
$depth = 512;
|
||||
$options = 0;
|
||||
$test = new Test();
|
||||
$test->curl->setDefaultJsonDecoder($assoc, $depth, $options);
|
||||
$test->server('json_response', 'GET');
|
||||
$this->assertTrue(is_array($test->curl->response));
|
||||
}
|
||||
|
||||
public function testJsonDecoder()
|
||||
{
|
||||
$data = array(
|
||||
'key' => 'Content-Type',
|
||||
@@ -1154,7 +1177,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertTrue(is_array($test->curl->response));
|
||||
}
|
||||
|
||||
public function testJSONContentTypeDetection()
|
||||
public function testJsonContentTypeDetection()
|
||||
{
|
||||
$json_content_types = array(
|
||||
'application/alto-costmap+json',
|
||||
|
||||
Reference in New Issue
Block a user