Forráskód Böngészése

Added option to return json as associative array

Jan Oris 9 éve
szülő
commit
c8d9740bca
3 módosított fájl, 14 hozzáadás és 5 törlés
  1. 6 0
      README.md
  2. 1 1
      composer.json
  3. 7 4
      src/Ixudra/Curl/Builder.php

+ 6 - 0
README.md

@@ -143,6 +143,12 @@ send. Currently, only the `GET` and `POST` method are supported. `PUT` and `DELE
         ->asJson()
         ->post();
 
+    // Send a POST request to: http://www.foo.com/bar with arguments 'foz' = 'baz' using JSON and return as associative array
+    $response = Curl::to('http://www.foo.com/bar')
+        ->withData( array( 'foz' => 'baz' ) )
+        ->asJson( true )
+        ->post();
+
     // Send a POST request to: http://www.foo.com/bar with arguments 'foz' = 'baz' using JSON over SSL
     $response = Curl::to('http://www.foo.com/bar')
         ->withData( array( 'foz' => 'baz' ) )

+ 1 - 1
composer.json

@@ -1,7 +1,7 @@
 {
     "name": "ixudra/curl",
     "description": "Custom PHP Curl library for the Laravel 5 framework - developed by Ixudra",
-    "version": "6.0.2",
+    "version": "6.0.3",
     "keywords": ["Ixudra", "Laravel", "Curl"],
     "homepage": "http://ixudra.be",
     "license": "MIT",

+ 7 - 4
src/Ixudra/Curl/Builder.php

@@ -20,6 +20,7 @@ class Builder {
     protected $packageOptions = array(
         'data'                  => array(),
         'asJson'                => false,
+        'returnAsArray'         => false,
     );
 
 
@@ -45,7 +46,7 @@ class Builder {
 
     /**
      *  Add GET or POST data to the request
-     * @param $data array   Array of data that is to be sent along wiht the request
+     * @param $data array   Array of data that is to be sent along with the request
      * @return $this
      */
     public function withData($data = array())
@@ -55,11 +56,13 @@ class Builder {
 
     /**
      *  Configure the package to encode and decode the request data
+     * @param $asArray boolean   Indicates whether or not the data should be returned as an array. Default: false
      * @return $this
      */
-    public function asJson()
+    public function asJson($asArray = false)
     {
-        return $this->withPackageOption( 'asJson', true );
+        return $this->withPackageOption( 'asJson', true )
+            ->withPackageOption( 'returnAsArray', $asArray );
     }
 
 //    /**
@@ -206,7 +209,7 @@ class Builder {
 
         // Decode the request if necessary
         if( $this->packageOptions[ 'asJson' ] ) {
-            $response = json_decode($response);
+            $response = json_decode( $response, $this->packageOptions[ 'returnAsArray' ] );
         }
 
         // Return the result