Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
530905c7c9 | ||
|
|
139b58019c | ||
|
|
8b47e051ba | ||
|
|
491064f40b | ||
|
|
fb241f6898 | ||
|
|
f6fc64103f | ||
|
|
11cf1eaa9f | ||
|
|
d5a7ad40bd | ||
|
|
8af870ff69 | ||
|
|
ae01978eb3 |
26
README.md
26
README.md
@@ -112,7 +112,13 @@ $response = Unirest\Request::post("http://httpbin.org/post", $headers, $body);
|
|||||||
|
|
||||||
### Authentication
|
### Authentication
|
||||||
|
|
||||||
Passing a username, password *(optional)*, defaults to Basic Authentication:
|
First, if you are using [Mashape][mashape-url]:
|
||||||
|
```php
|
||||||
|
// Mashape auth
|
||||||
|
Unirest\Request::setMashapeKey('<mashape_key>');
|
||||||
|
```
|
||||||
|
|
||||||
|
Otherwise, passing a username, password *(optional)*, defaults to Basic Authentication:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// basic auth
|
// basic auth
|
||||||
@@ -123,7 +129,7 @@ The third parameter, which is a bitmask, will Unirest which HTTP authentication
|
|||||||
|
|
||||||
If more than one bit is set, Unirest *(at PHP's libcurl level)* will first query the site to see what authentication methods it supports and then pick the best one you allow it to use. *For some methods, this will induce an extra network round-trip.*
|
If more than one bit is set, Unirest *(at PHP's libcurl level)* will first query the site to see what authentication methods it supports and then pick the best one you allow it to use. *For some methods, this will induce an extra network round-trip.*
|
||||||
|
|
||||||
**Supported Method**
|
**Supported Methods**
|
||||||
|
|
||||||
| Method | Description |
|
| Method | Description |
|
||||||
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||||
@@ -277,9 +283,21 @@ Unirest\Request::verifyPeer(false); // Disables SSL cert validation
|
|||||||
|
|
||||||
By default is `true`.
|
By default is `true`.
|
||||||
|
|
||||||
|
#### Utility Methods
|
||||||
|
|
||||||
|
```php
|
||||||
|
// alias for `curl_getinfo`
|
||||||
|
Unirest\Request::getInfo()
|
||||||
|
|
||||||
|
// returns internal cURL handle
|
||||||
|
Unirest\Request::getCurlHandle()
|
||||||
|
```
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
||||||
Made with ♥ from the [Mashape](https://www.mashape.com/) team
|
Made with ♥ from the [Mashape][mashape-url] team
|
||||||
|
|
||||||
|
[mashape-url]: https://www.mashape.com/
|
||||||
|
|
||||||
[license-url]: https://github.com/Mashape/unirest-php/blob/master/LICENSE
|
[license-url]: https://github.com/Mashape/unirest-php/blob/master/LICENSE
|
||||||
|
|
||||||
@@ -299,4 +317,4 @@ Made with ♥ from the [Mashape](https://www.mashape.com/) team
|
|||||||
[codeclimate-coverage]: https://img.shields.io/codeclimate/coverage/github/Mashape/unirest-php.svg?style=flat
|
[codeclimate-coverage]: https://img.shields.io/codeclimate/coverage/github/Mashape/unirest-php.svg?style=flat
|
||||||
|
|
||||||
[versioneye-url]: https://www.versioneye.com/user/projects/54b82450050646ca5c0001f3
|
[versioneye-url]: https://www.versioneye.com/user/projects/54b82450050646ca5c0001f3
|
||||||
[versioneye-image]: https://img.shields.io/versioneye/d/user/projects/54b82450050646ca5c0001f3.svg?style=flat
|
[versioneye-image]: https://img.shields.io/versioneye/d/php/mashape:unirest-php.svg?style=flat
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use Unirest\Response;
|
|||||||
|
|
||||||
class Request
|
class Request
|
||||||
{
|
{
|
||||||
|
private static $handle = null;
|
||||||
private static $jsonOpts = array();
|
private static $jsonOpts = array();
|
||||||
private static $verifyPeer = true;
|
private static $verifyPeer = true;
|
||||||
private static $socketTimeout = null;
|
private static $socketTimeout = null;
|
||||||
@@ -87,6 +88,20 @@ class Request
|
|||||||
return self::$defaultHeaders[$name] = $value;
|
return self::$defaultHeaders[$name] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a Mashape key to send on every request as a header
|
||||||
|
* Obtain your Mashape key by browsing one of your Mashape applications on https://www.mashape.com
|
||||||
|
*
|
||||||
|
* Note: Mashape provides 2 keys for each application: a 'Testing' and a 'Production' one.
|
||||||
|
* Be aware of which key you are using and do not share your Production key.
|
||||||
|
*
|
||||||
|
* @param string $key Mashape key
|
||||||
|
*/
|
||||||
|
public static function setMashapeKey($key)
|
||||||
|
{
|
||||||
|
return self::defaultHeader('X-Mashape-Key', $key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear all the default headers
|
* Clear all the default headers
|
||||||
*/
|
*/
|
||||||
@@ -309,15 +324,15 @@ class Request
|
|||||||
*/
|
*/
|
||||||
public static function send($method, $url, $body = null, $headers = array(), $username = null, $password = null)
|
public static function send($method, $url, $body = null, $headers = array(), $username = null, $password = null)
|
||||||
{
|
{
|
||||||
$ch = curl_init();
|
self::$handle = curl_init();
|
||||||
|
|
||||||
if ($method !== Method::GET) {
|
if ($method !== Method::GET) {
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method);
|
||||||
|
|
||||||
if (is_array($body) || $body instanceof \Traversable) {
|
if (is_array($body) || $body instanceof \Traversable) {
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, self::buildHTTPCurlQuery($body));
|
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, self::buildHTTPCurlQuery($body));
|
||||||
} else {
|
} else {
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
|
||||||
}
|
}
|
||||||
} elseif (is_array($body)) {
|
} elseif (is_array($body)) {
|
||||||
if (strpos($url, '?') !== false) {
|
if (strpos($url, '?') !== false) {
|
||||||
@@ -329,60 +344,75 @@ class Request
|
|||||||
$url .= urldecode(http_build_query(self::buildHTTPCurlQuery($body)));
|
$url .= urldecode(http_build_query(self::buildHTTPCurlQuery($body)));
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_URL, self::encodeUrl($url));
|
curl_setopt_array(self::$handle, array(
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
CURLOPT_URL => self::encodeUrl($url),
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
|
CURLOPT_FOLLOWLOCATION => true,
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, self::getFormattedHeaders($headers));
|
CURLOPT_MAXREDIRS => 10,
|
||||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
CURLOPT_HTTPHEADER => self::getFormattedHeaders($headers),
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, self::$verifyPeer);
|
CURLOPT_HEADER => true,
|
||||||
curl_setopt($ch, CURLOPT_ENCODING, ''); // If an empty string, '', is set, a header containing all supported encoding types is sent.
|
CURLOPT_SSL_VERIFYPEER => self::$verifyPeer,
|
||||||
|
// If an empty string, '', is set, a header containing all supported encoding types is sent
|
||||||
|
CURLOPT_ENCODING => ''
|
||||||
|
));
|
||||||
|
|
||||||
if (self::$socketTimeout !== null) {
|
if (self::$socketTimeout !== null) {
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
|
curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
// supporting deprecated http auth method
|
// supporting deprecated http auth method
|
||||||
if (!empty($username)) {
|
if (!empty($username)) {
|
||||||
curl_setopt($ch, CURLOPT_USERNAME, $username);
|
curl_setopt_array(self::$handle, array(
|
||||||
curl_setopt($ch, CURLOPT_PASSWORD, $password);
|
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
|
||||||
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
CURLOPT_USERPWD => $username . ':' . $password
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty(self::$auth['user'])) {
|
if (!empty(self::$auth['user'])) {
|
||||||
curl_setopt($ch, CURLOPT_USERNAME, self::$auth['user']);
|
curl_setopt_array(self::$handle, array(
|
||||||
curl_setopt($ch, CURLOPT_PASSWORD, self::$auth['pass']);
|
CURLOPT_HTTPAUTH => self::$auth['method'],
|
||||||
curl_setopt($ch, CURLOPT_HTTPAUTH, self::$auth['method']);
|
CURLOPT_USERPWD => self::$auth['user'] . ':' . self::$auth['pass']
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self::$proxy['address'] !== false) {
|
if (self::$proxy['address'] !== false) {
|
||||||
curl_setopt($ch, CURLOPT_PROXYTYPE, self::$proxy['type']);
|
curl_setopt_array(self::$handle, array(
|
||||||
curl_setopt($ch, CURLOPT_PROXY, self::$proxy['address']);
|
CURLOPT_PROXYTYPE => self::$proxy['type'],
|
||||||
curl_setopt($ch, CURLOPT_PROXYPORT, self::$proxy['port']);
|
CURLOPT_PROXY => self::$proxy['address'],
|
||||||
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, self::$proxy['tunnel']);
|
CURLOPT_PROXYPORT => self::$proxy['port'],
|
||||||
|
CURLOPT_HTTPPROXYTUNNEL => self::$proxy['tunnel'],
|
||||||
curl_setopt($ch, CURLOPT_PROXYAUTH, self::$proxy['auth']['method']);
|
CURLOPT_PROXYAUTH => self::$proxy['auth']['method'],
|
||||||
curl_setopt($ch, CURLOPT_PROXYUSERNAME, self::$proxy['auth']['user']);
|
CURLOPT_PROXYUSERPWD => self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass']
|
||||||
curl_setopt($ch, CURLOPT_PROXYPASSWORD, self::$proxy['auth']['pass']);
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec(self::$handle);
|
||||||
$error = curl_error($ch);
|
$error = curl_error(self::$handle);
|
||||||
|
$info = self::getInfo();
|
||||||
|
|
||||||
if ($error) {
|
if ($error) {
|
||||||
throw new \Exception($error);
|
throw new \Exception($error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split the full response in its headers and body
|
// Split the full response in its headers and body
|
||||||
$curl_info = curl_getinfo($ch);
|
$header_size = $info['header_size'];
|
||||||
$header_size = $curl_info['header_size'];
|
|
||||||
$header = substr($response, 0, $header_size);
|
$header = substr($response, 0, $header_size);
|
||||||
$body = substr($response, $header_size);
|
$body = substr($response, $header_size);
|
||||||
$httpCode = $curl_info['http_code'];
|
$httpCode = $info['http_code'];
|
||||||
|
|
||||||
return new Response($httpCode, $body, $header, self::$jsonOpts);
|
return new Response($httpCode, $body, $header, self::$jsonOpts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getInfo()
|
||||||
|
{
|
||||||
|
return curl_getinfo(self::$handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCurlHandle()
|
||||||
|
{
|
||||||
|
return self::$handle;
|
||||||
|
}
|
||||||
|
|
||||||
public static function getFormattedHeaders($headers)
|
public static function getFormattedHeaders($headers)
|
||||||
{
|
{
|
||||||
$formattedHeaders = array();
|
$formattedHeaders = array();
|
||||||
|
|||||||
@@ -64,6 +64,32 @@ class UnirestRequestTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse(array_key_exists('Hello', $properties));
|
$this->assertFalse(array_key_exists('Hello', $properties));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSetMashapeKey()
|
||||||
|
{
|
||||||
|
Unirest\Request::setMashapeKey('abcd');
|
||||||
|
$response = Unirest\Request::get('http://httpbin.org/get');
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response->code);
|
||||||
|
$headers = $response->body->headers;
|
||||||
|
$properties = get_object_vars($headers);
|
||||||
|
$this->assertTrue(array_key_exists('X-Mashape-Key', $properties));
|
||||||
|
$this->assertEquals('abcd', $headers->{'X-Mashape-Key'});
|
||||||
|
$response = Unirest\Request::get('http://httpbin.org/get');
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response->code);
|
||||||
|
$headers = $response->body->headers;
|
||||||
|
$properties = get_object_vars($headers);
|
||||||
|
$this->assertTrue(array_key_exists('X-Mashape-Key', $properties));
|
||||||
|
$this->assertEquals('abcd', $headers->{'X-Mashape-Key'});
|
||||||
|
Unirest\Request::clearDefaultHeaders();
|
||||||
|
$response = Unirest\Request::get('http://httpbin.org/get');
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response->code);
|
||||||
|
$headers = $response->body->headers;
|
||||||
|
$properties = get_object_vars($headers);
|
||||||
|
$this->assertFalse(array_key_exists('X-Mashape-Key', $properties));
|
||||||
|
}
|
||||||
|
|
||||||
public function testGzip()
|
public function testGzip()
|
||||||
{
|
{
|
||||||
$response = Unirest\Request::get('http://httpbin.org/gzip');
|
$response = Unirest\Request::get('http://httpbin.org/gzip');
|
||||||
@@ -71,13 +97,21 @@ class UnirestRequestTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(true, $args->gzipped);
|
$this->assertEquals(true, $args->gzipped);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testBasicAuthentication()
|
public function testBasicAuthenticationDeprecated()
|
||||||
{
|
{
|
||||||
$response = Unirest\Request::get('http://httpbin.org/get', array(), array(), 'user', 'password');
|
$response = Unirest\Request::get('http://httpbin.org/get', array(), array(), 'user', 'password');
|
||||||
$headers = $response->body->headers;
|
$headers = $response->body->headers;
|
||||||
$this->assertEquals('Basic dXNlcjpwYXNzd29yZA==', $headers->Authorization);
|
$this->assertEquals('Basic dXNlcjpwYXNzd29yZA==', $headers->Authorization);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testBasicAuthentication()
|
||||||
|
{
|
||||||
|
Unirest\Request::auth('user', 'password');
|
||||||
|
$response = Unirest\Request::get('http://httpbin.org/get');
|
||||||
|
|
||||||
|
$this->assertEquals('Basic dXNlcjpwYXNzd29yZA==', $response->body->headers->Authorization);
|
||||||
|
}
|
||||||
|
|
||||||
public function testCustomHeaders()
|
public function testCustomHeaders()
|
||||||
{
|
{
|
||||||
$response = Unirest\Request::get('http://httpbin.org/get', array(
|
$response = Unirest\Request::get('http://httpbin.org/get', array(
|
||||||
|
|||||||
Reference in New Issue
Block a user