7 Commits

Author SHA1 Message Date
Ahmad Nassri
530905c7c9 switch to using curl_setopt_array for better readability, added utility methods Fixes #66 2015-03-01 02:48:16 -05:00
Ahmad Nassri
139b58019c Merge pull request #68 from thibaultCha/feature/set-mashape-key
Native 'setMashapeKey' method for X-Mashape-Key header
2015-02-10 20:16:30 -08:00
thibaultCha
8b47e051ba Set Mashape Key by calling setDefaultHeader 2015-02-05 23:02:12 -08:00
thibaultCha
491064f40b Add test for setMashapeKey 2015-02-05 21:09:01 -08:00
thibaultCha
fb241f6898 Native 'setMashapeKey' method for X-Mashape-Key header 2015-02-05 20:49:06 -08:00
Ahmad Nassri
f6fc64103f Update README.md 2015-02-05 14:31:38 -05:00
Ahmad Nassri
11cf1eaa9f Update README.md 2015-02-05 14:04:02 -05:00
3 changed files with 110 additions and 33 deletions

View File

@@ -112,7 +112,13 @@ $response = Unirest\Request::post("http://httpbin.org/post", $headers, $body);
### 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
// 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.*
**Supported Method**
**Supported Methods**
| Method | Description |
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
@@ -277,9 +283,21 @@ Unirest\Request::verifyPeer(false); // Disables SSL cert validation
By default is `true`.
#### Utility Methods
```php
// alias for `curl_getinfo`
Unirest\Request::getInfo()
// returns internal cURL handle
Unirest\Request::getCurlHandle()
```
----
Made with &#9829; from the [Mashape](https://www.mashape.com/) team
Made with &#9829; from the [Mashape][mashape-url] team
[mashape-url]: https://www.mashape.com/
[license-url]: https://github.com/Mashape/unirest-php/blob/master/LICENSE
@@ -299,4 +317,4 @@ Made with &#9829; from the [Mashape](https://www.mashape.com/) team
[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-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

View File

@@ -7,6 +7,7 @@ use Unirest\Response;
class Request
{
private static $handle = null;
private static $jsonOpts = array();
private static $verifyPeer = true;
private static $socketTimeout = null;
@@ -87,6 +88,20 @@ class Request
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
*/
@@ -309,15 +324,15 @@ class Request
*/
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) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method);
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 {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
}
} elseif (is_array($body)) {
if (strpos($url, '?') !== false) {
@@ -329,57 +344,75 @@ class Request
$url .= urldecode(http_build_query(self::buildHTTPCurlQuery($body)));
}
curl_setopt($ch, CURLOPT_URL, self::encodeUrl($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, self::getFormattedHeaders($headers));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, self::$verifyPeer);
curl_setopt($ch, CURLOPT_ENCODING, ''); // If an empty string, '', is set, a header containing all supported encoding types is sent.
curl_setopt_array(self::$handle, array(
CURLOPT_URL => self::encodeUrl($url),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_HTTPHEADER => self::getFormattedHeaders($headers),
CURLOPT_HEADER => true,
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) {
curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout);
}
// supporting deprecated http auth method
if (!empty($username)) {
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt_array(self::$handle, array(
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $username . ':' . $password
));
}
if (!empty(self::$auth['user'])) {
curl_setopt($ch, CURLOPT_HTTPAUTH, self::$auth['method']);
curl_setopt($ch, CURLOPT_USERPWD, self::$auth['user'] . ':' . self::$auth['pass']);
curl_setopt_array(self::$handle, array(
CURLOPT_HTTPAUTH => self::$auth['method'],
CURLOPT_USERPWD => self::$auth['user'] . ':' . self::$auth['pass']
));
}
if (self::$proxy['address'] !== false) {
curl_setopt($ch, CURLOPT_PROXYTYPE, self::$proxy['type']);
curl_setopt($ch, CURLOPT_PROXY, self::$proxy['address']);
curl_setopt($ch, CURLOPT_PROXYPORT, self::$proxy['port']);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, self::$proxy['tunnel']);
curl_setopt($ch, CURLOPT_PROXYAUTH, self::$proxy['auth']['method']);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass']);
curl_setopt_array(self::$handle, array(
CURLOPT_PROXYTYPE => self::$proxy['type'],
CURLOPT_PROXY => self::$proxy['address'],
CURLOPT_PROXYPORT => self::$proxy['port'],
CURLOPT_HTTPPROXYTUNNEL => self::$proxy['tunnel'],
CURLOPT_PROXYAUTH => self::$proxy['auth']['method'],
CURLOPT_PROXYUSERPWD => self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass']
));
}
$response = curl_exec($ch);
$error = curl_error($ch);
$response = curl_exec(self::$handle);
$error = curl_error(self::$handle);
$info = self::getInfo();
if ($error) {
throw new \Exception($error);
}
// Split the full response in its headers and body
$curl_info = curl_getinfo($ch);
$header_size = $curl_info['header_size'];
$header_size = $info['header_size'];
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$httpCode = $curl_info['http_code'];
$httpCode = $info['http_code'];
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)
{
$formattedHeaders = array();

View File

@@ -64,6 +64,32 @@ class UnirestRequestTest extends \PHPUnit_Framework_TestCase
$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()
{
$response = Unirest\Request::get('http://httpbin.org/gzip');