From ba25e0304a9f7d1b2a681b0ee240cda3d201ce0b Mon Sep 17 00:00:00 2001 From: Ahmad Nassri Date: Thu, 15 Jan 2015 01:22:23 -0500 Subject: [PATCH] JSON Decoding options - users can now specify all the json_decode params to be used - additional method added: `Unirest\Request::defaultHeaders` --- README.md | 21 ++++++++++++++++- src/Unirest/Request.php | 41 +++++++++++++++++++++++++++++----- src/Unirest/Response.php | 9 ++++++-- tests/Unirest/ResponseTest.php | 28 +++++++++++++++++++++++ 4 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 tests/Unirest/ResponseTest.php diff --git a/README.md b/README.md index fd06ed7..d40b29c 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,17 @@ Upon recieving a response Unirest returns the result in the form of an Object, t You can set some advanced configuration to tune Unirest-PHP: +#### Custom JSON Decode Flags + +Unirest uses PHP's [JSON Extension](http://php.net/manual/en/book.json.php) for automatically decoding JSON responses. +sometime you may want to return associative arrays, limit the depth of recursion, or use any of the [customization flags](http://php.net/manual/en/json.constants.php#constant.json-hex-tag). + +To do so, simply set the desired options using the `jsonOpts` request method: + +```php +Unirest\Request::jsonOpts(true, 512, JSON_NUMERIC_CHECK & JSON_FORCE_OBJECT & JSON_UNESCAPED_SLASHES); +``` + #### Timeout You can set a custom timeout value (in **seconds**): @@ -163,6 +174,14 @@ Unirest\Request::defaultHeader("Header1", "Value1"); Unirest\Request::defaultHeader("Header2", "Value2"); ``` +You can do set default headers in bulk: + +```php +Unirest\Request::defaultHeaders(array( + "Header1" => "Value1", + "Header2" => "Value2" +)); + You can clear the default headers anytime with: ```php @@ -186,7 +205,7 @@ Licensed under [the MIT license](https://github.com/Mashape/unirest-php/blob/mas Created with love by [Mashape](https://www.mashape.com/). [gitter-url]: https://gitter.im/Mashape/unirest-php -[gitter-image]: https://badges.gitter.im/Mashape.png +[gitter-image]: https://badges.gitter.im/Join%20Chat.svg [composer-url]: http://badge.fury.io/ph/mashape%2Funirest-php [composer-image]: https://badge.fury.io/ph/mashape%2Funirest-php.svg diff --git a/src/Unirest/Request.php b/src/Unirest/Request.php index dfc4f45..4ee1e82 100644 --- a/src/Unirest/Request.php +++ b/src/Unirest/Request.php @@ -7,36 +7,66 @@ use Unirest\Response; class Request { + private static $jsonOpts = array(); private static $verifyPeer = true; private static $socketTimeout = null; private static $defaultHeaders = array(); + /** + * Set JSON decode mode + * + * @param bool $assoc When TRUE, returned objects will be converted into associative arrays. + * @param bool $depth User specified recursion depth. + * @param bool $options Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats) + */ + public static function jsonOpts($assoc = false, $depth = 512, $options = 0) + { + return self::$jsonOpts = array($assoc, $depth, $options); + } + /** * Verify SSL peer + * * @param bool $enabled enable SSL verification, by default is true */ public static function verifyPeer($enabled) { - self::$verifyPeer = $enabled; + return self::$verifyPeer = $enabled; } /** * Set a timeout + * * @param integer $seconds timeout value in seconds */ public static function timeout($seconds) { - self::$socketTimeout = $seconds; + return self::$socketTimeout = $seconds; + } + + /** + * Set default headers to send on every request + * + * @param array $headers headers array + */ + public static function defaultHeaders($headers) + { + foreach ($headers as $name => $value) { + self::$defaultHeaders[$name] = $value; + } + + return $headers; } /** * Set a new default header to send on every request + * * @param string $name header name * @param string $value header value */ public static function defaultHeader($name, $value) { - self::$defaultHeaders[$name] = $value; + return self::$defaultHeaders[$name] = $value; } /** @@ -44,11 +74,12 @@ class Request */ public static function clearDefaultHeaders() { - self::$defaultHeaders = array(); + return self::$defaultHeaders = array(); } /** * Send a GET request to a URL + * * @param string $url URL to send the GET request to * @param array $headers additional headers to send * @param mixed $parameters parameters to send in the querystring @@ -286,7 +317,7 @@ class Request $body = substr($response, $header_size); $httpCode = $curl_info['http_code']; - return new Response($httpCode, $body, $header); + return new Response($httpCode, $body, $header, self::$jsonOpts); } private static function getArrayFromQuerystring($query) diff --git a/src/Unirest/Response.php b/src/Unirest/Response.php index d0b4b0b..e3b85cc 100644 --- a/src/Unirest/Response.php +++ b/src/Unirest/Response.php @@ -13,14 +13,19 @@ class Response * @param int $code response code of the cURL request * @param string $raw_body the raw body of the cURL response * @param string $headers raw header string from cURL response + * @param array $json_args arguments to pass to json_decode function */ - public function __construct($code, $raw_body, $headers) + public function __construct($code, $raw_body, $headers, $json_args = array()) { $this->code = $code; $this->headers = $this->parseHeaders($headers); $this->raw_body = $raw_body; $this->body = $raw_body; - $json = json_decode($raw_body); + + // make sure raw_body is the first argument + array_unshift($json_args, $raw_body); + + $json = call_user_func_array('json_decode', $json_args); if (json_last_error() === JSON_ERROR_NONE) { $this->body = $json; diff --git a/tests/Unirest/ResponseTest.php b/tests/Unirest/ResponseTest.php new file mode 100644 index 0000000..b8774ba --- /dev/null +++ b/tests/Unirest/ResponseTest.php @@ -0,0 +1,28 @@ +assertEquals($response->body['a'], 1); + } + + public function testJSONAObjects() + { + $opts = Unirest\Request::jsonOpts(false); + $response = new Unirest\Response(200, '{"a":1,"b":2,"c":3,"d":4,"e":5}', '', $opts); + + $this->assertEquals($response->body->a, 1); + } + + public function testJSONOpts() + { + $opts = Unirest\Request::jsonOpts(false, 512, JSON_NUMERIC_CHECK); + $response = new Unirest\Response(200, '{"number": 1234567890}', '', $opts); + + $this->assertSame($response->body->number, 1234567890); + } +}