8000 Add support for proxy types · adamwiw/codebird-php@5c41f3f · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c41f3f

Browse files
committed
Add support for proxy types
See jublo#143.
1 parent 6066b11 commit 5c41f3f

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ codebird-php - changelog
22
========================
33

44
3.1.0 (not yet released)
5+
+ #143 Add support for proxy types
56

67
3.0.0 (2016-01-01)
78
+ Add unit testing suite

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,3 +840,10 @@ You may also use an authenticated proxy. Use the following call:
840840
$cb->setProxy('<host>', '<port>');
841841
$cb->setProxyAuthentication('<username>:<password>');
842842
```
843+
844+
By default, a HTTP proxy is assumed. To use a different proxy type,
845+
use the corresponding [`CURLPROXY_*` constants](http://php.net/curl_setopt), like this:
846+
847+
```php
848+
$cb->setProxy('<host>', '<port>', CURLPROXY_SOCKS5);
849+
```

src/codebird.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,15 +713,25 @@ public function setReturnFormat($return_format)
713713
/**
714714
* Sets the proxy
715715
*
716-
* @param string $host Proxy host
717-
* @param int $port Proxy port
716+
* @param string $host Proxy host
717+
* @param int $port Proxy port
718+
* @param int optional $type Proxy type, defaults to HTTP
718719
*
719720
* @return void
720721
*/
721-
public function setProxy($host, $port)
722+
public function setProxy($host, $port, $type = CURLPROXY_HTTP)
722723
{
723724
$this->_proxy['host'] = $host;
724725
$this->_proxy['port'] = (int) $port;
726+
727+
static $types = [
728+
CURLPROXY_HTTP, CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
729+
CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
730+
];
731+
if (! in_array($type, $types)) {
732+
throw new \Exception('Invalid proxy type specified.');
733+
}
734+
$this->_proxy['type'] = $type;
725735
}
726736

727737
/**
@@ -1053,7 +1063,7 @@ protected function _getCurlInitialization($url)
10531063
);
10541064

10551065
if ($this->_hasProxy()) {
1056-
$this->_curl_setopt($connection, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
1066+
$this->_curl_setopt($connection, CURLOPT_PROXYTYPE, $this->_getProxyType());
10571067
$this->_curl_setopt($connection, CURLOPT_PROXY, $this->_getProxyHost());
10581068
$this->_curl_setopt($connection, CURLOPT_PROXYPORT, $this->_getProxyPort());
10591069

@@ -1159,6 +1169,16 @@ protected function _getProxyAuthentication()
11591169
return $this->_getProxyData('authentication');
11601170
}
11611171

1172+
/**
1173+
* Gets the proxy type
1174+
*
1175+
* @return string The proxy type
1176+
*/
1177+
protected function _getProxyType()
1178+
{
1179+
return $this->_getProxyData('type');
1180+
}
1181+
11621182
/**
11631183
* Gets data from the proxy configuration
11641184
*

test/setter_tests.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,23 @@ public function testSetProxy()
150150
$cb->setProxy(& B9D6 #39;127.0.0.1', '8888');
151151
$this->assertEquals('127.0.0.1', $cb->get('_proxy')['host']);
152152
$this->assertEquals('8888', $cb->get('_proxy')['port']);
153+
$this->assertEquals(CURLPROXY_HTTP, $cb->get('_proxy')['type']);
154+
155+
$cb->setProxy('127.0.0.1', '8888', CURLPROXY_SOCKS5);
156+
$this->assertEquals('127.0.0.1', $cb->get('_proxy')['host']);
157+
$this->assertEquals('8888', $cb->get('_proxy')['port']);
158+
$this->assertEquals(CURLPROXY_SOCKS5, $cb->get('_proxy')['type']);
159+
}
160+
161+
/**
162+
* Tests setProxy
163+
* @expectedException \Exception
164+
* @expectedExceptionMessage Invalid proxy type specified.
165+
*/
166+
public function testSetProxy2()
167+
{
168+
$cb = new CodebirdT();
169+
$cb->setProxy('127.0.0.1', '8888', 1);
153170
}
154171

155172
/**

0 commit comments

Comments
 (0)
0