Skip to content

Commit d98ba1b

Browse files
authored
Merge pull request php-curl-class#531 from zachborboa/proxy
Implement methods to use proxy
2 parents 34355b1 + 86e12e3 commit d98ba1b

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ Curl::setMaxFilesize($bytes)
271271
Curl::setOpt($option, $value)
272272
Curl::setOpts($options)
273273
Curl::setPort($port)
274+
Curl::setProxy($proxy, $port = null, $username = null, $password = null)
275+
Curl::setProxyAuth($auth)
276+
Curl::setProxyTunnel($tunnel)
277+
Curl::setProxyType($type)
274278
Curl::setReferer($referer)
275279
Curl::setReferrer($referrer)
276280
Curl::setRetry($mixed)
@@ -280,6 +284,7 @@ Curl::setUserAgent($user_agent)
280284
Curl::setXmlDecoder($mixed)
281285
Curl::success($callback)
282286
Curl::unsetHeader($key)
287+
Curl::unsetProxy()
283288
Curl::verbose($on = true, $output = STDERR)
284289
MultiCurl::__construct($base_url = null)
285290
MultiCurl::__destruct()

src/Curl/Curl.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,79 @@ public function setOpts($options)
10151015
return true;
10161016
}
10171017

1018+
/**
1019+
* Set Proxy
1020+
*
1021+
* Set an HTTP proxy to tunnel requests through.
1022+
*
1023+
* @access public
1024+
* @param $proxy - The HTTP proxy to tunnel requests through. May include port number.
1025+
* @param $port - The port number of the proxy to connect to. This port number can also be set in $proxy.
1026+
* @param $username - The username to use for the connection to the proxy.
1027+
* @param $password - The password to use for the connection to the proxy.
1028+
*/
1029+
public function setProxy($proxy, $port = null, $username = null, $password = null)
1030+
{
1031+
$this->setOpt(CURLOPT_PROXY, $proxy);
1032+
if ($port !== null) {
1033+
$this->setOpt(CURLOPT_PROXYPORT, $port);
1034+
}
1035+
if ($username !== null && $password !== null) {
1036+
$this->setOpt(CURLOPT_PROXYUSERPWD, $username . ':' . $password);
1037+
}
1038+
}
1039+
1040+
/**
1041+
* Set Proxy Auth
1042+
*
1043+
* Set the HTTP authentication method(s) to use for the proxy connection.
1044+
*
1045+
* @access public
1046+
* @param $auth
1047+
*/
1048+
public function setProxyAuth($auth)
1049+
{
1050+
$this-setOpt(CURLOPT_PROXYAUTH, $auth);
1051+
}
1052+
1053+
/**
1054+
* Set Proxy Type
1055+
*
1056+
* Set the proxy protocol type.
1057+
*
1058+
* @access public
1059+
* @param $type
1060+
*/
1061+
public function setProxyType($type)
1062+
{
1063+
$this->setOpt(CURLOPT_PROXYTYPE, $type);
1064+
}
1065+
1066+
/**
1067+
* Set Proxy Tunnel
1068+
*
1069+
* Set the proxy to tunnel through HTTP proxy.
1070+
*
1071+
* @access public
1072+
* @param $tunnel boolean
1073+
*/
1074+
public function setProxyTunnel($tunnel)
1075+
{
1076+
$this->setOpt(CURLOPT_HTTPPROXYTUNNEL, $tunnel);
1077+
}
1078+
1079+
/**
1080+
* Unset Proxy
1081+
*
1082+
* Disable use of the proxy.
1083+
*
1084+
* @access public
1085+
*/
1086+
public function unsetProxy()
1087+
{
1088+
$this->setOpt(CURLOPT_PROXY, null);
1089+
}
1090+
10181091
/**
10191092
* Set Referer
10201093
*

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3688,4 +3688,17 @@ public function testMock()
36883688

36893689
$this->assertEquals('[]', $curl->getRawResponse());
36903690
}
3691+
3692+
public function testProxySettings()
3693+
{
3694+
$curl = new Curl();
3695+
$curl->setProxy('proxy.example.com', '1080', 'username', 'password');
3696+
3697+
$this->assertEquals('proxy.example.com', $curl->getOpt(CURLOPT_PROXY));
3698+
$this->assertEquals('1080', $curl->getOpt(CURLOPT_PROXYPORT));
3699+
$this->assertEquals('username:password', $curl->getOpt(CURLOPT_PROXYUSERPWD));
3700+
3701+
$curl->unsetProxy();
3702+
$this->assertNull($curl->getOpt(CURLOPT_PROXY));
3703+
}
36913704
}

0 commit comments

Comments
 (0)