mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 04:01:47 +00:00
Merge pull request #568 from zachborboa/master
Add proxy support for MultiCurl
This commit is contained in:
@@ -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)
|
||||
```
|
||||
|
||||
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user