Selaa lähdekoodia

Updated package to allow JSON requests

Jan Oris 10 vuotta sitten
vanhempi
commit
1bc7f00cb0
4 muutettua tiedostoa jossa 132 lisäystä ja 34 poistoa
  1. 83 3
      README.md
  2. 2 6
      composer.json
  3. 27 17
      src/Ixudra/Curl/Curl.php
  4. 20 8
      src/Ixudra/Curl/CurlService.php

+ 83 - 3
README.md

@@ -1,7 +1,87 @@
-Curl
-====
+Ixudra/Curl
+=============
 
-Custom PHP Curl library - developed by Ixudra.
+Custom PHP curl library for the Laravel 4 framework - developed by Ixudra.
 
 This package can be used by anyone at any given time, but keep in mind that it is optimized for my personal custom workflow. It may not suit your project perfectly and modifications may be in order.
 
+
+
+
+## Installation
+
+Pull this package in through Composer.
+
+```js
+
+    {
+        "require": {
+            "ixudra/curl": "0.1.*"
+        }
+    }
+
+```
+
+Add the service provider to your `config/app.php` file:
+
+```php
+
+    providers       => array(
+
+        //...
+        'Ixudra\Curl\CurlServiceProvider',
+
+    )
+
+```
+
+Add the facade to your `config/app.php` file:
+
+```php
+
+    facades         => array(
+
+        //...
+        'Curl'          => 'Ixudra\Curl\Facades\Curl',
+
+    )
+
+```
+
+
+
+
+## Usage
+
+### GET requests
+
+The package provides an easy interface for sending CURL requests from your application. Optionally, you can also include several `GET` parameters that will automatically be added to the base URL by the package automatically. Lastly, the package also has a parameter that allows you to easily mark a request as a json requests. The package will automatically handle the conversion from and to json to PHP wherever needed. The default value of this parameter is `false`:
+
+```php
+
+    Curl::get('http://www.foo.com/bar');
+
+    Curl::get('http://www.foo.com/bar', array('foz' => 'baz'));
+
+    Curl::get('http://www.foo.com/bar', array('foz' => 'baz'), true);
+
+```
+
+The package will subsequently following URL: `http://www.foo.com/bar?foz=baz`.
+
+
+### POST requests
+
+The package also allows you to send `POST` requests for your application. The first and second parameter are identical to the `Curl::get()` method. The `POST` parameters can be past on as the third parameter. The fourth and last parameter can be used to mark the request as a json requests. The package will automatically handle the conversion from and to json to PHP wherever needed.
+
+```php
+
+    Curl::post('http://www.foo.com/bar', array(), array('fow' => 'baw'));
+
+    Curl::post('http://www.foo.com/bar', array('foz' => 'baz'), array('fow' => 'baw'));
+
+    Curl::post('http://www.foo.com/bar', array('foz' => 'baz'), array('fow' => 'baw'), true);
+
+```
+
+That's all there is to it! Have fun!

+ 2 - 6
composer.json

@@ -1,7 +1,7 @@
 {
     "name": "ixudra/curl",
     "description": "Custom PHP Curl library - developer by Ixudra",
-    "version": "0.1.1",
+    "version": "0.2.*",
     "keywords": ["Ixudra", "Laravel", "Curl"],
     "homepage": "http://ixudra.be",
     "license": "MIT",
@@ -15,14 +15,10 @@
 
     "require": {
         "php": ">=5.3.0",
-        "illuminate/support": ">=4.1"
+        "illuminate/support": "4.2.*"
     },
 
     "autoload": {
-        "classmap": [
-            "src/migrations"
-        ],
-
         "psr-0": {
             "Ixudra\\Curl\\": "src/"
         }

+ 27 - 17
src/Ixudra/Curl/Curl.php

@@ -3,9 +3,9 @@
 
 class Curl {
 
-    protected $_curlObject = null;
+    protected $curlObject = null;
 
-    protected $_options = array(
+    protected $options = array(
         'RETURN_TRANSFER'       => true,
         'FAIL_ON_ERROR'         => true,
         'FOLLOW_LOCATION'       => false,
@@ -20,42 +20,52 @@ class Curl {
 
     public function send()
     {
-        $this->_curlObject = curl_init();
-        $options = $this->_forgeOptions();
-        curl_setopt_array( $this->_curlObject, $options );
+        $this->curlObject = curl_init();
+        $options = $this->forgeOptions();
+        curl_setopt_array( $this->curlObject, $options );
 
-        $response = curl_exec($this->_curlObject);
-        curl_close( $this->_curlObject );
+        $response = curl_exec( $this->curlObject );
+        curl_close( $this->curlObject );
 
         return $response;
     }
 
-    public function setUrl($url)
+    public function setUrl($baseUrl, $getParameters = array())
     {
-        $this->_options['URL'] = $url;
+        $parameterString = '';
+        if( is_array($getParameters) && count($getParameters) != 0 ) {
+            $parameterString = '?'. http_build_query($getParameters);
+        }
+
+        return $this->options[ 'URL' ] = $baseUrl . $parameterString;
     }
 
     public function setMethod($method)
     {
-        $this->_options['POST'] = $method;
+        $this->options[ 'POST' ] = $method;
     }
 
     public function setPostParameters(array $parameters)
     {
-        $this->_options['POST'] = true;
-        $this->_options['POST_FIELDS'] = $parameters;
+        $this->options[ 'POST' ] = true;
+        $this->options[ 'POST_FIELDS' ] = $parameters;
+    }
+
+    public function addOption($key, $value)
+    {
+        $this->options[ $key ] = $value;
     }
 
-    protected function _forgeOptions()
+    protected function forgeOptions()
     {
         $results = array();
-        foreach( $this->_options as $key => $value ) {
+        foreach( $this->options as $key => $value ) {
             $array_key = constant( 'CURLOPT_' . str_replace('_', '', $key) );
 
-            if( $key == 'POST_FIELDS' && is_array($value) ) {
-                $results[$array_key] = http_build_query($value, NULL, '&');
+            if( $key == 'POST_FIELDS' && is_array( $value ) ) {
+                $results[ $array_key ] = http_build_query( $value, null, '&' );
             } else {
-                $results[$array_key] = $value;
+                $results[ $array_key ] = $value;
             }
         }
 

+ 20 - 8
src/Ixudra/Curl/CurlService.php

@@ -3,24 +3,36 @@
 
 class CurlService {
 
-    public function get($url)
+    public function get($url, $getParameters = array(), $isJson = false)
     {
         $curl = new Curl();
-        $curl->setUrl( $url );
+        $curl->setUrl( $url, $getParameters );
 
-        $response = $curl->send();
-
-        return $response;
+        return $this->send( $curl, $isJson );
     }
 
-    public function post($url, $parameters)
+    public function post($url, $getParameters = array(), $postParameters, $isJson = false)
     {
         $curl = new Curl();
-        $curl->setUrl( $url );
+        $curl->setUrl( $url, $getParameters );
         $curl->setMethod( true );
-        $curl->setPostParameters( $parameters );
 
+        if( $isJson ) {
+            $postParameters = json_encode($postParameters);
+            $curl->addOption( 'HTTP_HEADER', array('Content-Type: application/json') );
+        }
+
+        $curl->setPostParameters( $postParameters );
+
+        return $this->send( $curl, $isJson );
+    }
+
+    protected function send($curl, $isJson)
+    {
         $response = $curl->send();
+        if( $isJson ) {
+            $response = json_decode($response);
+        }
 
         return $response;
     }