gzip support; default request headers; customizable timeout; basic authentication; bugfixes; closes #5 #15 #16 #18
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
{
|
||||
"name" : "mashape/unirest-php",
|
||||
"description" : "Unirest PHP",
|
||||
"keywords" : ["rest", "curl"],
|
||||
"keywords" : ["rest", "curl", "http", "client"],
|
||||
"type" : "library",
|
||||
"homepage" : "http://unirest.io#php",
|
||||
"homepage" : "https://github.com/Mashape/unirest-php",
|
||||
"license" : "MIT",
|
||||
"authors" : [
|
||||
{
|
||||
@@ -17,6 +17,9 @@
|
||||
"ext-curl" : "*",
|
||||
"ext-json" : "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"vierbergenlars/simpletest": "*"
|
||||
},
|
||||
"autoload" : {
|
||||
"psr-0" : {
|
||||
"Unirest" : "lib"
|
||||
@@ -25,4 +28,4 @@
|
||||
"support" : {
|
||||
"email" : "support@mashape.com"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,46 @@ use Unirest\HttpResponse;
|
||||
|
||||
class Unirest
|
||||
{
|
||||
|
||||
private static $socketTimeout = null;
|
||||
private static $defaultHeaders = array();
|
||||
|
||||
/**
|
||||
* Set a timeout
|
||||
* @param integer $seconds Timeout value in seconds
|
||||
*/
|
||||
public static function timeout($seconds) {
|
||||
Unirest::$socketTimeout = $seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a new default header to send on every request
|
||||
* @param string $name Header name
|
||||
* @param string $value Header value
|
||||
*/
|
||||
public static function defaultHeader($name, $value) {
|
||||
Unirest::$defaultHeaders[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all the default headers
|
||||
*/
|
||||
public static function clearDefaultHeaders() {
|
||||
Unirest::$defaultHeaders = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a GET request to a URL
|
||||
* @param string $url URL to send the GET request to
|
||||
* @param array $headers Additional headers to send
|
||||
* @param mixed $parameters Parameters to send in the querystring
|
||||
* @param string $username Basic Authentication username
|
||||
* @param string $password Basic Authentication password
|
||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
||||
*/
|
||||
public static function get($url, $headers = array())
|
||||
public static function get($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL)
|
||||
{
|
||||
return Unirest::request(HttpMethod::GET, $url, NULL, $headers);
|
||||
return Unirest::request(HttpMethod::GET, $url, $parameters, $headers, $username, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -21,22 +52,27 @@ class Unirest
|
||||
* @param string $url URL to send the POST request to
|
||||
* @param array $headers Additional headers to send
|
||||
* @param mixed $body POST body data
|
||||
* @param string $username Basic Authentication username
|
||||
* @param string $password Basic Authentication password
|
||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
||||
*/
|
||||
public static function post($url, $headers = array(), $body = NULL)
|
||||
public static function post($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
||||
{
|
||||
return Unirest::request(HttpMethod::POST, $url, $body, $headers);
|
||||
return Unirest::request(HttpMethod::POST, $url, $body, $headers, $username, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send DELETE request to a URL
|
||||
* @param string $url URL to send the DELETE request to
|
||||
* @param array $headers Additional headers to send
|
||||
* @param mixed $body DELETE body data
|
||||
* @param string $username Basic Authentication username
|
||||
* @param string $password Basic Authentication password
|
||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
||||
*/
|
||||
public static function delete($url, $headers = array())
|
||||
public static function delete($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
||||
{
|
||||
return Unirest::request(HttpMethod::DELETE, $url, NULL, $headers);
|
||||
return Unirest::request(HttpMethod::DELETE, $url, $body, $headers, $username, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,11 +80,13 @@ class Unirest
|
||||
* @param string $url URL to send the PUT request to
|
||||
* @param array $headers Additional headers to send
|
||||
* @param mixed $body PUT body data
|
||||
* @param string $username Basic Authentication username
|
||||
* @param string $password Basic Authentication password
|
||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
||||
*/
|
||||
public static function put($url, $headers = array(), $body = NULL)
|
||||
public static function put($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
||||
{
|
||||
return Unirest::request(HttpMethod::PUT, $url, $body, $headers);
|
||||
return Unirest::request(HttpMethod::PUT, $url, $body, $headers, $username, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,11 +94,19 @@ class Unirest
|
||||
* @param string $url URL to send the PATCH request to
|
||||
* @param array $headers Additional headers to send
|
||||
* @param mixed $body PATCH body data
|
||||
* @param string $username Basic Authentication username
|
||||
* @param string $password Basic Authentication password
|
||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
||||
*/
|
||||
public static function patch($url, $headers = array(), $body = NULL)
|
||||
public static function patch($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
||||
{
|
||||
return Unirest::request(HttpMethod::PATCH, $url, $body, $headers);
|
||||
return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password);
|
||||
}
|
||||
|
||||
private static function getHeader($key, $val) {
|
||||
$key = trim(strtolower($key));
|
||||
if ($key == "user-agent" || $key == "expect") continue;
|
||||
return $key . ": " . $val;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,38 +115,61 @@ class Unirest
|
||||
* @param string $url URL to send the request to
|
||||
* @param mixed $body Request body
|
||||
* @param array $headers Additional headers to send
|
||||
* @param string $username Basic Authentication username
|
||||
* @param string $password Basic Authentication password
|
||||
* @throws Exception If a cURL error occurs
|
||||
* @return \Unireset\HttpResponse \Unirest\HttpResponse object
|
||||
*/
|
||||
private static function request($httpMethod, $url, $body = NULL, $headers = array())
|
||||
private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL)
|
||||
{
|
||||
if ($headers == NULL) $headers = array();
|
||||
$lowercaseHeaders = array();
|
||||
foreach ($headers as $key => $val) {
|
||||
$key = trim(strtolower($key));
|
||||
if ($key == "user-agent" || $key == "expect") continue;
|
||||
$lowercaseHeaders[] = $key . ": " . $val;
|
||||
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
||||
}
|
||||
$lowercaseHeaders[] = "user-agent: unirest-php/1.0";
|
||||
foreach (Unirest::$defaultHeaders as $key => $val) {
|
||||
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
||||
}
|
||||
|
||||
$lowercaseHeaders[] = "user-agent: unirest-php/1.1";
|
||||
$lowercaseHeaders[] = "expect:";
|
||||
|
||||
|
||||
$ch = curl_init();
|
||||
if ($httpMethod != HttpMethod::GET) {
|
||||
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
||||
curl_setopt ($ch, CURLOPT_POSTFIELDS, $body);
|
||||
} else if (is_array($body)) {
|
||||
if (strpos($url,'?') !== false) {
|
||||
$url .= "&";
|
||||
} else {
|
||||
$url .= "?";
|
||||
}
|
||||
|
||||
foreach ($body as $parameter => $val) {
|
||||
$url .= $parameter . "=" . $val . "&";
|
||||
}
|
||||
$url = substr($url, 0, strlen($url) - 1);
|
||||
}
|
||||
|
||||
curl_setopt ($ch, CURLOPT_URL , Unirest::encodeUrl($url));
|
||||
|
||||
curl_setopt ($ch, CURLOPT_URL, Unirest::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, $lowercaseHeaders);
|
||||
curl_setopt ($ch, CURLOPT_HEADER, true);
|
||||
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
|
||||
curl_setopt ($ch, CURLOPT_ENCODING, ""); // If an empty string, "", is set, a header containing all supported encoding types is sent.
|
||||
if (Unirest::$socketTimeout != null) {
|
||||
curl_setopt ($ch, CURLOPT_TIMEOUT, Unirest::$socketTimeout);
|
||||
}
|
||||
if (!empty($username)) {
|
||||
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . ((empty($password)) ? "" : $password));
|
||||
}
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$error = curl_error($ch);
|
||||
if ($error) {
|
||||
throw new \Exception($error);
|
||||
throw new Exception($error);
|
||||
}
|
||||
|
||||
// Split the full response in its headers and body
|
||||
|
||||
1
test/.gitignore
vendored
Normal file
1
test/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
simpletest
|
||||
30
test/Unirest.php
Normal file
30
test/Unirest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
echo "Running the Unirest-PHP bindings test suite.\n".
|
||||
"If you're trying to use the Unirest-PHP bindings you'll probably want ".
|
||||
"to require('lib/Unirest.php'); instead of this file\n";
|
||||
|
||||
$ok = @include_once(dirname(__FILE__).'/simpletest/autorun.php');
|
||||
if (!$ok) {
|
||||
$ok = @include_once(dirname(__FILE__).'/../vendor/vierbergenlars/simpletest/autorun.php');
|
||||
}
|
||||
if (!$ok) {
|
||||
echo "MISSING DEPENDENCY: The Unirest-PHP test cases depend on SimpleTest. ".
|
||||
"Download it at <http://www.simpletest.org/>, and either install it ".
|
||||
"in your PHP include_path or put it in the test/ directory.\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Throw an exception on any error
|
||||
function exception_error_handler($errno, $errstr, $errfile, $errline) {
|
||||
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
|
||||
}
|
||||
|
||||
set_error_handler('exception_error_handler');
|
||||
error_reporting(E_ALL | E_STRICT);
|
||||
|
||||
require_once(dirname(__FILE__) . '/../lib/Unirest.php');
|
||||
|
||||
require_once(dirname(__FILE__) . '/Unirest/UnirestTest.php');
|
||||
|
||||
?>
|
||||
135
test/Unirest/UnirestTest.php
Normal file
135
test/Unirest/UnirestTest.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
class UnirestTest extends UnitTestCase
|
||||
{
|
||||
public function testGet()
|
||||
{
|
||||
$response = Unirest::get("http://httpbin.org/get?name=Mark", array( "Accept" => "application/json" ),
|
||||
array(
|
||||
"nick" => "thefosk"
|
||||
));
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
|
||||
$args = $response->body->args;
|
||||
$this->assertEqual("Mark", $args->name);
|
||||
$this->assertEqual("thefosk", $args->nick);
|
||||
}
|
||||
|
||||
public function testPost()
|
||||
{
|
||||
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
|
||||
array(
|
||||
"name" => "Mark",
|
||||
"nick" => "thefosk"
|
||||
));
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
|
||||
$form = $response->body->form;
|
||||
$this->assertEqual("Mark", $form->name);
|
||||
$this->assertEqual("thefosk", $form->nick);
|
||||
}
|
||||
|
||||
public function testPut()
|
||||
{
|
||||
$response = Unirest::put("http://httpbin.org/put", array( "Accept" => "application/json" ),
|
||||
array(
|
||||
"name" => "Mark",
|
||||
"nick" => "thefosk"
|
||||
));
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
|
||||
$form = $response->body->form;
|
||||
$this->assertEqual("Mark", $form->name);
|
||||
$this->assertEqual("thefosk", $form->nick);
|
||||
}
|
||||
|
||||
public function testPatch()
|
||||
{
|
||||
$response = Unirest::patch("http://httpbin.org/patch", array( "Accept" => "application/json" ),
|
||||
array(
|
||||
"name" => "Mark",
|
||||
"nick" => "thefosk"
|
||||
));
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
|
||||
$form = $response->body->form;
|
||||
$this->assertEqual("Mark", $form->name);
|
||||
$this->assertEqual("thefosk", $form->nick);
|
||||
}
|
||||
|
||||
public function testDelete()
|
||||
{
|
||||
$response = Unirest::delete("http://httpbin.org/delete", array( "Accept" => "application/json", "Content-Type" => "application/x-www-form-urlencoded" ),
|
||||
array(
|
||||
"name" => "Mark",
|
||||
"nick" => "thefosk"
|
||||
));
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
$data = $response->body->data;
|
||||
$this->assertFalse(empty($data));
|
||||
}
|
||||
|
||||
public function testTimeoutFail()
|
||||
{
|
||||
Unirest::timeout(1);
|
||||
|
||||
$this->expectException();
|
||||
$response = Unirest::get("http://httpbin.org/delay/3");
|
||||
}
|
||||
|
||||
public function testTimeoutSuccess()
|
||||
{
|
||||
Unirest::timeout(3);
|
||||
|
||||
$response = Unirest::get("http://httpbin.org/delay/1");
|
||||
$this->assertEqual(200, $response->code);
|
||||
}
|
||||
|
||||
public function testDefaultHeader()
|
||||
{
|
||||
Unirest::defaultHeader("Hello", "custom");
|
||||
$response = Unirest::get("http://httpbin.org/get");
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
$headers = $response->body->headers;
|
||||
$properties = get_object_vars($headers);
|
||||
$this->assertTrue(array_key_exists("Hello", $properties));
|
||||
$this->assertEqual("custom", $headers->Hello);
|
||||
|
||||
$response = Unirest::get("http://httpbin.org/get");
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
$headers = $response->body->headers;
|
||||
$properties = get_object_vars($headers);
|
||||
$this->assertTrue(array_key_exists("Hello", $properties));
|
||||
$this->assertEqual("custom", $headers->Hello);
|
||||
|
||||
Unirest::clearDefaultHeaders();
|
||||
$response = Unirest::get("http://httpbin.org/get");
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
$headers = $response->body->headers;
|
||||
$properties = get_object_vars($headers);
|
||||
$this->assertFalse(array_key_exists("Hello", $properties));
|
||||
}
|
||||
|
||||
public function testGzip()
|
||||
{
|
||||
$response = Unirest::get("http://httpbin.org/gzip");
|
||||
$args = $response->body;
|
||||
$this->assertEqual(true, $args->gzipped);
|
||||
}
|
||||
|
||||
public function testBasicAuthentication()
|
||||
{
|
||||
$response = Unirest::get("http://httpbin.org/get", null, null, "user", "password");
|
||||
$headers = $response->body->headers;
|
||||
$this->assertEqual("Basic dXNlcjpwYXNzd29yZA==", $headers->Authorization);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user