mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-25 02:29:08 +00:00
Fix #331: Add methods to use proxy
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user