From 86e12e3b911684f63eafe5db1a67ba57b5c9ccdb Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Tue, 17 Jul 2018 04:29:25 -0700 Subject: [PATCH] Fix #331: Add methods to use proxy --- README.md | 5 ++ src/Curl/Curl.php | 73 +++++++++++++++++++++++++ tests/PHPCurlClass/PHPCurlClassTest.php | 13 +++++ 3 files changed, 91 insertions(+) diff --git a/README.md b/README.md index 8ee9b78..419c5d8 100644 --- a/README.md +++ b/README.md @@ -271,6 +271,10 @@ Curl::setMaxFilesize($bytes) Curl::setOpt($option, $value) Curl::setOpts($options) Curl::setPort($port) +Curl::setProxy($proxy, $port = null, $username = null, $password = null) +Curl::setProxyAuth($auth) +Curl::setProxyTunnel($tunnel) +Curl::setProxyType($type) Curl::setReferer($referer) Curl::setReferrer($referrer) Curl::setRetry($mixed) @@ -280,6 +284,7 @@ Curl::setUserAgent($user_agent) Curl::setXmlDecoder($mixed) Curl::success($callback) Curl::unsetHeader($key) +Curl::unsetProxy() Curl::verbose($on = true, $output = STDERR) MultiCurl::__construct($base_url = null) MultiCurl::__destruct() diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 20b3f8f..953b294 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -1015,6 +1015,79 @@ class Curl return true; } + /** + * Set Proxy + * + * Set an HTTP proxy to tunnel requests through. + * + * @access public + * @param $proxy - The HTTP proxy to tunnel requests through. May include port number. + * @param $port - The port number of the proxy to connect to. This port number can also be set in $proxy. + * @param $username - The username to use for the connection to the proxy. + * @param $password - The password to use for the connection to the proxy. + */ + public function setProxy($proxy, $port = null, $username = null, $password = null) + { + $this->setOpt(CURLOPT_PROXY, $proxy); + if ($port !== null) { + $this->setOpt(CURLOPT_PROXYPORT, $port); + } + if ($username !== null && $password !== null) { + $this->setOpt(CURLOPT_PROXYUSERPWD, $username . ':' . $password); + } + } + + /** + * Set Proxy Auth + * + * Set the HTTP authentication method(s) to use for the proxy connection. + * + * @access public + * @param $auth + */ + public function setProxyAuth($auth) + { + $this-setOpt(CURLOPT_PROXYAUTH, $auth); + } + + /** + * Set Proxy Type + * + * Set the proxy protocol type. + * + * @access public + * @param $type + */ + public function setProxyType($type) + { + $this->setOpt(CURLOPT_PROXYTYPE, $type); + } + + /** + * Set Proxy Tunnel + * + * Set the proxy to tunnel through HTTP proxy. + * + * @access public + * @param $tunnel boolean + */ + public function setProxyTunnel($tunnel) + { + $this->setOpt(CURLOPT_HTTPPROXYTUNNEL, $tunnel); + } + + /** + * Unset Proxy + * + * Disable use of the proxy. + * + * @access public + */ + public function unsetProxy() + { + $this->setOpt(CURLOPT_PROXY, null); + } + /** * Set Referer * diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index d7e4ca6..6266b42 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -3688,4 +3688,17 @@ class CurlTest extends \PHPUnit\Framework\TestCase $this->assertEquals('[]', $curl->getRawResponse()); } + + public function testProxySettings() + { + $curl = new Curl(); + $curl->setProxy('proxy.example.com', '1080', 'username', 'password'); + + $this->assertEquals('proxy.example.com', $curl->getOpt(CURLOPT_PROXY)); + $this->assertEquals('1080', $curl->getOpt(CURLOPT_PROXYPORT)); + $this->assertEquals('username:password', $curl->getOpt(CURLOPT_PROXYUSERPWD)); + + $curl->unsetProxy(); + $this->assertNull($curl->getOpt(CURLOPT_PROXY)); + } }