Fix #331: Add methods to use proxy

This commit is contained in:
Zach Borboa
2018-07-17 04:29:25 -07:00
parent 34355b1eff
commit 86e12e3b91
3 changed files with 91 additions and 0 deletions
+5
View File
@@ -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()
+73
View File
@@ -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
*
+13
View File
@@ -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));
}
}