closes #47
This commit is contained in:
Ahmad Nassri
2015-02-05 11:33:59 -05:00
parent 5fa02336f9
commit c5be546d1b
2 changed files with 48 additions and 1 deletions

View File

@@ -1,11 +1,11 @@
# Unirest for PHP [![Build Status][travis-image]][travis-url] [![version][packagist-version]][packagist-url]
[![License][packagist-license]][license-url]
[![Downloads][packagist-downloads]][packagist-url]
[![Code Climate][codeclimate-quality]][codeclimate-url]
[![Coverage Status][codeclimate-coverage]][codeclimate-url]
[![Dependencies][versioneye-image]][versioneye-url]
[![Gitter][gitter-image]][gitter-url]
[![License][packagist-license]][license-url]
Unirest is a set of lightweight HTTP libraries available in [multiple languages](http://unirest.io).
@@ -174,6 +174,26 @@ You can set a custom timeout value (in **seconds**):
Unirest\Request::timeout(5); // 5s timeout
```
#### Proxy
Set the proxy to use for the upcoming request.
you can also set the proxy type to be one of `CURLPROXY_HTTP`, `CURLPROXY_HTTP_1_0`, `CURLPROXY_SOCKS4`, `CURLPROXY_SOCKS5`, `CURLPROXY_SOCKS4A`, and `CURLPROXY_SOCKS5_HOSTNAME`.
*check the [cURL docs](http://curl.haxx.se/libcurl/c/CURLOPT_PROXYTYPE.html) for more info*.
```php
// quick setup with default port: 1080
Unirest\Request::proxy('10.10.10.1');
// custom port and proxy type
Unirest\Request::proxy('10.10.10.1', 8080, CURLPROXY_HTTP)l
// enable tunneling
Unirest\Request::proxy('10.10.10.1', 8080, CURLPROXY_HTTP, true);
```
#### Default Request Headers
You can set default headers that will be sent on every request:

View File

@@ -7,6 +7,10 @@ use Unirest\Response;
class Request
{
private static $proxyPort = false;
private static $proxyType = CURLPROXY_HTTP;
private static $proxyTunnel = false;
private static $proxyAddress = false;
private static $jsonOpts = array();
private static $verifyPeer = true;
private static $socketTimeout = null;
@@ -77,6 +81,22 @@ class Request
return self::$defaultHeaders = array();
}
/**
* Set proxy to use
*
* @param string $address proxy address
* @param string $port proxy port
* @param string $port proxy type (Available options for this are CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5, CURLPROXY_SOCKS4A and CURLPROXY_SOCKS5_HOSTNAME)
* @param string $tunnel enable/disable tunneling
*/
public static function proxy($address, $port = 1080, $type = CURLPROXY_HTTP, $tunnel = false)
{
self::$proxyType = $type;
self::$proxyPort = $port;
self::$proxyTunnel = $tunnel;
self::$proxyAddress = $address;
}
/**
* Send a GET request to a URL
*
@@ -283,6 +303,13 @@ class Request
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . ((empty($password)) ? '' : $password));
}
if (self::$proxyAddress) {
curl_setopt($ch, CURLOPT_PROXYTYPE, self::$proxyType);
curl_setopt($ch, CURLOPT_PROXY, self::$proxyAddress);
curl_setopt($ch, CURLOPT_PROXYPORT, self::$proxyPort);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, self::$proxyTunnel);
}
$response = curl_exec($ch);
$error = curl_error($ch);