diff --git a/README.md b/README.md index e03c9c2..c0fdfa8 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,10 @@ MultiCurl::setJsonDecoder($mixed) MultiCurl::setOpt($option, $value) MultiCurl::setOpts($options) MultiCurl::setPort($port) +MultiCurl::setProxy($proxy, $port = null, $username = null, $password = null) +MultiCurl::setProxyAuth($auth) +MultiCurl::setProxyTunnel($tunnel = true) +MultiCurl::setProxyType($type) MultiCurl::setReferer($referer) MultiCurl::setReferrer($referrer) MultiCurl::setRetry($mixed) @@ -330,6 +334,7 @@ MultiCurl::setXmlDecoder($mixed) MultiCurl::start() MultiCurl::success($callback) MultiCurl::unsetHeader($key) +MultiCurl::unsetProxy() MultiCurl::verbose($on = true, $output = STDERR) ``` diff --git a/examples/multi_curl_proxy.php b/examples/multi_curl_proxy.php new file mode 100644 index 0000000..45e3d16 --- /dev/null +++ b/examples/multi_curl_proxy.php @@ -0,0 +1,13 @@ +setProxy('someproxy.com', '9999', 'username', 'password'); +$multi_curl->setProxyTunnel(); +$multi_curl->addGet('https://httpbin.org/ip'); +$multi_curl->complete(function ($instance) { + var_dump($instance->response); +}); +$multi_curl->start(); diff --git a/examples/multi_curl_proxy_socks5.php b/examples/multi_curl_proxy_socks5.php new file mode 100644 index 0000000..e0d50f6 --- /dev/null +++ b/examples/multi_curl_proxy_socks5.php @@ -0,0 +1,16 @@ +setProxy('127.0.0.1:8080'); +$multi_curl->setProxyType(CURLPROXY_SOCKS5); +$multi_curl->addGet('https://httpbin.org/ip'); +$multi_curl->complete(function ($instance) { + var_dump($instance->response); +}); +$multi_curl->start(); diff --git a/src/Curl/MultiCurl.php b/src/Curl/MultiCurl.php index b597df6..025710f 100644 --- a/src/Curl/MultiCurl.php +++ b/src/Curl/MultiCurl.php @@ -549,6 +549,79 @@ class MultiCurl } } + /** + * 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 = true) + { + $this->setOpt(CURLOPT_HTTPPROXYTUNNEL, $tunnel); + } + + /** + * Unset Proxy + * + * Disable use of the proxy. + * + * @access public + */ + public function unsetProxy() + { + $this->setOpt(CURLOPT_PROXY, null); + } + /** * Set Opt * diff --git a/tests/PHPCurlClass/PHPMultiCurlClassTest.php b/tests/PHPCurlClass/PHPMultiCurlClassTest.php index 00debce..7549584 100644 --- a/tests/PHPCurlClass/PHPMultiCurlClassTest.php +++ b/tests/PHPCurlClass/PHPMultiCurlClassTest.php @@ -2876,4 +2876,17 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase $multi_curl->start(); $this->assertTrue($post_complete_called); } + + public function testProxySettings() + { + $multi_curl = new MultiCurl(); + $multi_curl->setProxy('proxy.example.com', '1080', 'username', 'password'); + + $this->assertEquals('proxy.example.com', $multi_curl->getOpt(CURLOPT_PROXY)); + $this->assertEquals('1080', $multi_curl->getOpt(CURLOPT_PROXYPORT)); + $this->assertEquals('username:password', $multi_curl->getOpt(CURLOPT_PROXYUSERPWD)); + + $multi_curl->unsetProxy(); + $this->assertNull($multi_curl->getOpt(CURLOPT_PROXY)); + } }