Merge pull request #568 from zachborboa/master

Add proxy support for MultiCurl
This commit is contained in:
Zach Borboa
2019-01-05 01:01:47 -08:00
committed by GitHub
5 changed files with 120 additions and 0 deletions
+5
View File
@@ -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)
```
+13
View File
@@ -0,0 +1,13 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
use \Curl\Curl;
$multi_curl = new MultiCurl();
$multi_curl->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();
+16
View File
@@ -0,0 +1,16 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
use \Curl\MultiCurl;
// If needed, start a SOCKS 5 proxy tunnel:
// $ ssh -D 8080 -C -N -v user@example.com
$multi_curl = new MultiCurl();
$multi_curl->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();
+73
View File
@@ -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
*
@@ -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));
}
}