diff --git a/examples/proxy.php b/examples/proxy.php new file mode 100644 index 000000000..884276f76 --- /dev/null +++ b/examples/proxy.php @@ -0,0 +1,22 @@ +body ); + +// Now let's make a request via a proxy. +$options = array( + 'proxy' => '127.0.0.1:8080', // syntax: host:port, eg 12.13.14.14:8080 or someproxy.com:3128 +); +$request = Requests::get('http://httpbin.org/ip', array(), $options ); + +// Compare with previous result +var_dump( $request->body ); \ No newline at end of file diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index b6f3136dd..3442b7b46 100755 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -84,7 +84,7 @@ public function __construct() { */ public function request($url, $headers = array(), $data = array(), $options = array()) { $this->setup_handle($url, $headers, $data, $options); - + $options['hooks']->dispatch('curl.before_send', array(&$this->fp)); if ($options['filename'] !== false) { @@ -106,6 +106,17 @@ public function request($url, $headers = array(), $data = array(), $options = ar curl_setopt($this->fp, CURLOPT_SSL_VERIFYHOST, 0); } + // Proxy support + if ( isset( $options['proxy'] ) ) { + curl_setopt( $this->fp, CURLOPT_PROXYTYPE, CURLPROXY_HTTP ); + curl_setopt( $this->fp, CURLOPT_PROXY, $options['proxy'] ); + + if ( isset( $options['proxy_username'] ) && isset( $options['proxy_password'] ) ) { + curl_setopt( $this->fp, CURLOPT_PROXYAUTH, CURLAUTH_ANY ); + curl_setopt( $this->fp, CURLOPT_PROXYUSERPWD, $options['proxy_username'] . ':' . $options['proxy_password'] ); + } + } + $response = curl_exec($this->fp); $options['hooks']->dispatch('curl.after_send', array(&$fake_headers)); diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index 4b37839c0..527e75624 100755 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -78,14 +78,23 @@ public function request($url, $headers = array(), $data = array(), $options = ar else { $remote_socket = 'tcp://' . $host; } - + + $proxy = isset( $options['proxy'] ); + $proxy_auth = $proxy && isset( $options['proxy_username'] ) && isset( $options['proxy_password'] ); + if (!isset($url_parts['port'])) { $url_parts['port'] = 80; } $remote_socket .= ':' . $url_parts['port']; set_error_handler(array($this, 'connect_error_handler'), E_WARNING | E_NOTICE); - $fp = stream_socket_client($remote_socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $context); + + if ( $proxy ) + $fp = stream_socket_client( 'tcp://' . $options['proxy'], $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $context); + else + $fp = stream_socket_client( $remote_socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $context); + + restore_error_handler(); if (!$fp) { @@ -105,14 +114,18 @@ public function request($url, $headers = array(), $data = array(), $options = ar case Requests::POST: case Requests::PUT: case Requests::PATCH: - if (isset($url_parts['path'])) { - $path = $url_parts['path']; - if (isset($url_parts['query'])) { - $path .= '?' . $url_parts['query']; + if( $proxy ) { + $path = $url; + } else { + if (isset($url_parts['path'])) { + $path = $url_parts['path']; + if (isset($url_parts['query'])) { + $path .= '?' . $url_parts['query']; + } + } + else { + $path = '/'; } - } - else { - $path = '/'; } $out = $options['type'] . " $path HTTP/1.0\r\n"; if (is_array($data)) { @@ -131,7 +144,11 @@ public function request($url, $headers = array(), $data = array(), $options = ar case Requests::HEAD: case Requests::GET: case Requests::DELETE: - $get = self::format_get($url_parts, $data); + if( $proxy ) { + $get = $url; + } else { + $get = self::format_get($url_parts, $data); + } $out = $options['type'] . " $get HTTP/1.0\r\n"; break; } @@ -140,6 +157,9 @@ public function request($url, $headers = array(), $data = array(), $options = ar if ($url_parts['port'] !== 80) { $out .= ":{$url_parts['port']}"; } + + if( $proxy_auth ) + $out .= 'Proxy-Authorization: Basic ' . base64_encode( $options['proxy_username'] . ':' . $options['proxy_password'] ) . "\r\n"; $out .= "\r\n";