瀏覽代碼

Improved proxy functionality

Jan Oris 7 年之前
父節點
當前提交
cba2654bf2
共有 4 個文件被更改,包括 44 次插入33 次删除
  1. 4 0
      CHANGELOG.md
  2. 25 1
      README.md
  3. 1 1
      composer.json
  4. 14 31
      src/Builder.php

+ 4 - 0
CHANGELOG.md

@@ -2,6 +2,10 @@
 
 All Notable changes to `ixudra/curl` will be documented in this file
 
+## 6.15.0 - 2017-11-06
+### Added
+- Added withProxy method
+
 ## 6.14.0 - 2017-10-30
 ### Added
 - Laravel auto-discovery

+ 25 - 1
README.md

@@ -301,7 +301,7 @@ Alternatively, you can use the `withHeaders()` to combine multiple headers into
 
 ### Specifying the content type
 
-Sending custom headers is easy with the `withcontentType()` method. Multiple calls can be chained together to add multiple headers to the request:
+Sending custom headers is easy with the `withContentType()` method. Multiple calls can be chained together to add multiple headers to the request:
 
 ```php
 
@@ -315,6 +315,30 @@ Sending custom headers is easy with the `withcontentType()` method. Multiple cal
 ```
 
 
+### Using proxies
+
+If you need to send your requests via a proxy, you can use the 'withProxy()' method. The method takes five parameters:
+
+- proxy url (required)
+- port (optional)
+- type of proxy scheme (optional, e.g. `http://`, `https://`, ...)
+- username (optional)
+- password (optional)
+
+Optional parameters will be ignored if not filled in.
+
+```php
+
+    use Ixudra\Curl\Facades\Curl;
+
+    // Send a GET request to: http://www.foo.com/bar with a json content type
+    $response = Curl::to('http://foo.com/bar')
+        ->withProxy('192.168.1.1', 80, 'http://', 'Foo', 'Bar')
+        ->get();
+
+```
+
+
 ### Sending files via Curl
 
 For sending files via a POST request, you can use the `withFile` method to correctly format a request before sending:

+ 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.14.0",
+    "version": "6.15.0",
     "keywords": ["Ixudra", "Laravel", "Curl"],
     "homepage": "http://ixudra.be",
     "license": "MIT",

+ 14 - 31
src/Builder.php

@@ -287,44 +287,27 @@ class Builder {
     /**
      * Enable Proxy for the cURL request
      *
-     * @param   string $proxy    Proxy hostname
+     * @param   string $proxy       Hostname
+     * @param   string $port        Port to be used
+     * @param   string $type        Scheme to be used by the proxy
+     * @param   string $username    Authentication username
+     * @param   string $password    Authentication password
      * @return Builder
      */
-    public function enableProxy($proxy)
+    public function withProxy($proxy, $port = '', $type = '', $username = '', $password = '')
     {
-        return $this->withPackageOption( 'enableProxy', true )
-            ->withOption( 'PROXY', $proxy );
-    }
+        $this->withOption( 'PROXY', $proxy );
 
-    /**
-     * Allow User to specify Proxy port seperately. It will work only if Proxy
-     * is enabled using `enableProxy` method.
-     *
-     * @param   int $proxyPort     Proxy port
-     * @return Builder
-     */
-    public function withProxyPort($proxyPort)
-    {
-        if ( $this->packageOptions[ 'enableProxy' ] ) {
-            $this->withOption( 'PROXYPORT', $proxyPort );
+        if( !empty($port) ) {
+            $this->withOption( 'PROXYPORT', $proxy );
         }
 
-        return $this;
-    }
+        if( !empty($type) ) {
+            $this->withOption( 'PROXYTYPE', $type );
+        }
 
-    /**
-     * Allow User to specify Proxy authentication credentials. It will add
-     * authentication credentials only if Proxy is enabled using `enableProxy`
-     * method.
-     *
-     * @param   string $proxyUsername     Proxy Authentication Username
-     * @param   string $proxyPassword     Proxy Authentication Password
-     * @return Builder
-     */
-    public function withProxyAuth($proxyUsername, $proxyPassword)
-    {
-        if ( $this->packageOptions[ 'enableProxy' ] ) {
-            $this->withOption( 'PROXYUSERPWD', $proxyUsername . ':' . $proxyPassword );
+        if( !empty($username) && !empty($password) ) {
+            $this->withOption( 'PROXYUSERPWD', $username .':'. $password );
         }
 
         return $this;