Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b46a35cf6a | |||
|
|
87642e3e05 | ||
|
|
3ba32276cc | ||
|
|
e65aef7c8f | ||
|
|
655607d739 | ||
|
|
14aa9aa691 | ||
|
|
842c0f242d | ||
|
|
5d304c6f6c | ||
|
|
53656bf519 | ||
|
|
889ca14f5f | ||
|
|
45274fd536 | ||
|
|
a3446f6d99 | ||
|
|
e9ea4dbee2 | ||
|
|
d71cd15d4a | ||
|
|
2d0cd4c8d9 | ||
|
|
c01f685e3b | ||
|
|
28080c975c |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,6 +1,9 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
|
.idea
|
||||||
build
|
build
|
||||||
composer.lock
|
composer.lock
|
||||||
composer.phar
|
composer.phar
|
||||||
coverage
|
coverage
|
||||||
vendor
|
vendor
|
||||||
|
|
||||||
|
composer
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ To install unirest-php with Composer, just add the following to your `composer.j
|
|||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"mashape/unirest-php": "2.*"
|
"mashape/unirest-php": "3.*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "mashape/unirest-php",
|
"name": "bitmotor/unirest-php",
|
||||||
"description": "Unirest PHP",
|
"description": "Unirest PHP",
|
||||||
"keywords": ["rest", "curl", "http", "https", "client"],
|
"keywords": ["rest", "curl", "http", "https", "client"],
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"homepage": "https://github.com/Mashape/unirest-php",
|
"homepage": "https://git.steeeg.de/bitmotor/unirest-php",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"author": "Mashape <opensource@mashape.com> (https://www.mashape.com)",
|
"author": "Mashape <opensource@mashape.com> (https://www.mashape.com)",
|
||||||
"require": {
|
"require": {
|
||||||
@@ -18,9 +18,7 @@
|
|||||||
"codeclimate/php-test-reporter": "0.1.*"
|
"codeclimate/php-test-reporter": "0.1.*"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-0": {
|
"psr-4": { "Unirest\\": "src/Unirest" }
|
||||||
"Unirest": "src"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"support": {
|
"support": {
|
||||||
"email": "opensource@mashape.com"
|
"email": "opensource@mashape.com"
|
||||||
|
|||||||
@@ -2,10 +2,6 @@
|
|||||||
|
|
||||||
namespace Unirest;
|
namespace Unirest;
|
||||||
|
|
||||||
use Unirest\Method as Method;
|
|
||||||
use Unirest\Response as Response;
|
|
||||||
use Unirest\Exception as Exception;
|
|
||||||
|
|
||||||
class Request
|
class Request
|
||||||
{
|
{
|
||||||
private static $cookie = null;
|
private static $cookie = null;
|
||||||
@@ -86,6 +82,7 @@ class Request
|
|||||||
* Set default headers to send on every request
|
* Set default headers to send on every request
|
||||||
*
|
*
|
||||||
* @param array $headers headers array
|
* @param array $headers headers array
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function defaultHeaders($headers)
|
public static function defaultHeaders($headers)
|
||||||
{
|
{
|
||||||
@@ -159,7 +156,7 @@ class Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a coockie string for enabling coockie handling
|
* Set a cookie string for enabling cookie handling
|
||||||
*
|
*
|
||||||
* @param string $cookie
|
* @param string $cookie
|
||||||
*/
|
*/
|
||||||
@@ -169,7 +166,7 @@ class Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a coockie file path for enabling coockie handling
|
* Set a cookie file path for enabling cookie handling
|
||||||
*
|
*
|
||||||
* $cookieFile must be a correct path with write permission
|
* $cookieFile must be a correct path with write permission
|
||||||
*
|
*
|
||||||
@@ -232,7 +229,7 @@ class Request
|
|||||||
* @param mixed $parameters parameters to send in the querystring
|
* @param mixed $parameters parameters to send in the querystring
|
||||||
* @param string $username Authentication username (deprecated)
|
* @param string $username Authentication username (deprecated)
|
||||||
* @param string $password Authentication password (deprecated)
|
* @param string $password Authentication password (deprecated)
|
||||||
* @return Unirest\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public static function get($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
public static function get($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
@@ -246,7 +243,7 @@ class Request
|
|||||||
* @param mixed $parameters parameters to send in the querystring
|
* @param mixed $parameters parameters to send in the querystring
|
||||||
* @param string $username Basic Authentication username (deprecated)
|
* @param string $username Basic Authentication username (deprecated)
|
||||||
* @param string $password Basic Authentication password (deprecated)
|
* @param string $password Basic Authentication password (deprecated)
|
||||||
* @return Unirest\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public static function head($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
public static function head($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
@@ -260,7 +257,7 @@ class Request
|
|||||||
* @param mixed $parameters parameters to send in the querystring
|
* @param mixed $parameters parameters to send in the querystring
|
||||||
* @param string $username Basic Authentication username
|
* @param string $username Basic Authentication username
|
||||||
* @param string $password Basic Authentication password
|
* @param string $password Basic Authentication password
|
||||||
* @return Unirest\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public static function options($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
public static function options($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
@@ -274,7 +271,7 @@ class Request
|
|||||||
* @param mixed $parameters parameters to send in the querystring
|
* @param mixed $parameters parameters to send in the querystring
|
||||||
* @param string $username Basic Authentication username (deprecated)
|
* @param string $username Basic Authentication username (deprecated)
|
||||||
* @param string $password Basic Authentication password (deprecated)
|
* @param string $password Basic Authentication password (deprecated)
|
||||||
* @return Unirest\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public static function connect($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
public static function connect($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
@@ -288,7 +285,7 @@ class Request
|
|||||||
* @param mixed $body POST body data
|
* @param mixed $body POST body data
|
||||||
* @param string $username Basic Authentication username (deprecated)
|
* @param string $username Basic Authentication username (deprecated)
|
||||||
* @param string $password Basic Authentication password (deprecated)
|
* @param string $password Basic Authentication password (deprecated)
|
||||||
* @return Unirest\Response response
|
* @return Response response
|
||||||
*/
|
*/
|
||||||
public static function post($url, $headers = array(), $body = null, $username = null, $password = null)
|
public static function post($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
@@ -302,7 +299,7 @@ class Request
|
|||||||
* @param mixed $body DELETE body data
|
* @param mixed $body DELETE body data
|
||||||
* @param string $username Basic Authentication username (deprecated)
|
* @param string $username Basic Authentication username (deprecated)
|
||||||
* @param string $password Basic Authentication password (deprecated)
|
* @param string $password Basic Authentication password (deprecated)
|
||||||
* @return Unirest\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public static function delete($url, $headers = array(), $body = null, $username = null, $password = null)
|
public static function delete($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
@@ -316,7 +313,7 @@ class Request
|
|||||||
* @param mixed $body PUT body data
|
* @param mixed $body PUT body data
|
||||||
* @param string $username Basic Authentication username (deprecated)
|
* @param string $username Basic Authentication username (deprecated)
|
||||||
* @param string $password Basic Authentication password (deprecated)
|
* @param string $password Basic Authentication password (deprecated)
|
||||||
* @return Unirest\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public static function put($url, $headers = array(), $body = null, $username = null, $password = null)
|
public static function put($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
@@ -330,7 +327,7 @@ class Request
|
|||||||
* @param mixed $body PATCH body data
|
* @param mixed $body PATCH body data
|
||||||
* @param string $username Basic Authentication username (deprecated)
|
* @param string $username Basic Authentication username (deprecated)
|
||||||
* @param string $password Basic Authentication password (deprecated)
|
* @param string $password Basic Authentication password (deprecated)
|
||||||
* @return Unirest\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public static function patch($url, $headers = array(), $body = null, $username = null, $password = null)
|
public static function patch($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
@@ -344,7 +341,7 @@ class Request
|
|||||||
* @param mixed $body TRACE body data
|
* @param mixed $body TRACE body data
|
||||||
* @param string $username Basic Authentication username (deprecated)
|
* @param string $username Basic Authentication username (deprecated)
|
||||||
* @param string $password Basic Authentication password (deprecated)
|
* @param string $password Basic Authentication password (deprecated)
|
||||||
* @return Unirest\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public static function trace($url, $headers = array(), $body = null, $username = null, $password = null)
|
public static function trace($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
@@ -385,21 +382,28 @@ class Request
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a cURL request
|
* Send a cURL request
|
||||||
* @param Unirest\Method|string $method HTTP method to use
|
* @param \Unirest\Method|string $method HTTP method to use
|
||||||
* @param string $url URL to send the request to
|
* @param string $url URL to send the request to
|
||||||
* @param mixed $body request body
|
* @param mixed $body request body
|
||||||
* @param array $headers additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @param string $username Authentication username (deprecated)
|
* @param string $username Authentication username (deprecated)
|
||||||
* @param string $password Authentication password (deprecated)
|
* @param string $password Authentication password (deprecated)
|
||||||
* @throws Unirest\Exception if a cURL error occurs
|
* @throws \Unirest\Exception if a cURL error occurs
|
||||||
* @return Unirest\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
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)
|
||||||
{
|
{
|
||||||
self::$handle = curl_init();
|
self::$handle = curl_init();
|
||||||
|
|
||||||
if ($method !== Method::GET) {
|
if ($method !== Method::GET) {
|
||||||
curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method);
|
if ($method === Method::POST) {
|
||||||
|
curl_setopt(self::$handle, CURLOPT_POST, true);
|
||||||
|
} else {
|
||||||
|
if ($method === Method::HEAD) {
|
||||||
|
curl_setopt(self::$handle, CURLOPT_NOBODY, true);
|
||||||
|
}
|
||||||
|
curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method);
|
||||||
|
}
|
||||||
|
|
||||||
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
|
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
|
||||||
} elseif (is_array($body)) {
|
} elseif (is_array($body)) {
|
||||||
@@ -504,7 +508,7 @@ class Request
|
|||||||
{
|
{
|
||||||
$formattedHeaders = array();
|
$formattedHeaders = array();
|
||||||
|
|
||||||
$combinedHeaders = array_change_key_case(array_merge((array) $headers, self::$defaultHeaders));
|
$combinedHeaders = array_change_key_case(array_merge(self::$defaultHeaders, (array) $headers));
|
||||||
|
|
||||||
foreach ($combinedHeaders as $key => $val) {
|
foreach ($combinedHeaders as $key => $val) {
|
||||||
$formattedHeaders[] = self::getHeaderString($key, $val);
|
$formattedHeaders[] = self::getHeaderString($key, $val);
|
||||||
@@ -551,7 +555,7 @@ class Request
|
|||||||
$query = '?' . http_build_query(self::getArrayFromQuerystring($query));
|
$query = '?' . http_build_query(self::getArrayFromQuerystring($query));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($port && $port[0] !== ':') {
|
if ($port && (!is_array($port) || $port[0] !== ':')) {
|
||||||
$port = ':' . $port;
|
$port = ':' . $port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,12 @@ class UnirestRequestTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertObjectHasAttribute('header2', $response->body->headers);
|
$this->assertObjectHasAttribute('header2', $response->body->headers);
|
||||||
$this->assertEquals('world', $response->body->headers->header2);
|
$this->assertEquals('world', $response->body->headers->header2);
|
||||||
|
|
||||||
|
$response = Request::get('http://mockbin.com/request', ['header1' => 'Custom value']);
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response->code);
|
||||||
|
$this->assertObjectHasAttribute('header1', $response->body->headers);
|
||||||
|
$this->assertEquals('Custom value', $response->body->headers->header1);
|
||||||
|
|
||||||
Request::clearDefaultHeaders();
|
Request::clearDefaultHeaders();
|
||||||
|
|
||||||
$response = Request::get('http://mockbin.com/request');
|
$response = Request::get('http://mockbin.com/request');
|
||||||
@@ -245,6 +251,16 @@ class UnirestRequestTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('John', $response->body->queryString->name[1]);
|
$this->assertEquals('John', $response->body->queryString->name[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HEAD
|
||||||
|
public function testHead()
|
||||||
|
{
|
||||||
|
$response = Request::head('http://mockbin.com/request?name=Mark', array(
|
||||||
|
'Accept' => 'application/json'
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response->code);
|
||||||
|
}
|
||||||
|
|
||||||
// POST
|
// POST
|
||||||
public function testPost()
|
public function testPost()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user