Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d7aabfa47 | ||
|
|
a7797328fa | ||
|
|
6bb573b1f6 | ||
|
|
2fb13154e2 |
@@ -1,15 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Unirest;
|
namespace Unirest;
|
||||||
|
|
||||||
interface HttpMethod
|
interface HttpMethod
|
||||||
{
|
{
|
||||||
|
|
||||||
const DELETE = "DELETE";
|
const DELETE = "DELETE";
|
||||||
const GET = "GET";
|
const GET = "GET";
|
||||||
const POST = "POST";
|
const POST = "POST";
|
||||||
const PUT = "PUT";
|
const PUT = "PUT";
|
||||||
const PATCH = "PATCH";
|
const PATCH = "PATCH";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,77 +1,77 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Unirest;
|
namespace Unirest;
|
||||||
|
|
||||||
class HttpResponse
|
class HttpResponse
|
||||||
|
{
|
||||||
|
|
||||||
|
private $code;
|
||||||
|
private $raw_body;
|
||||||
|
private $body;
|
||||||
|
private $headers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $code response code of the cURL request
|
||||||
|
* @param string $raw_body the raw body of the cURL response
|
||||||
|
* @param string $headers raw header string from cURL response
|
||||||
|
*/
|
||||||
|
public function __construct($code, $raw_body, $headers)
|
||||||
{
|
{
|
||||||
|
$this->code = $code;
|
||||||
private $code;
|
$this->headers = $this->get_headers_from_curl_response($headers);
|
||||||
private $raw_body;
|
$this->raw_body = $raw_body;
|
||||||
private $body;
|
$this->body = $raw_body;
|
||||||
private $headers;
|
$json = json_decode($raw_body);
|
||||||
|
|
||||||
/**
|
if (json_last_error() == JSON_ERROR_NONE) {
|
||||||
* @param int $code response code of the cURL request
|
$this->body = $json;
|
||||||
* @param string $raw_body the raw body of the cURL response
|
}
|
||||||
* @param string $headers raw header string from cURL response
|
}
|
||||||
*/
|
|
||||||
public function __construct($code, $raw_body, $headers)
|
/**
|
||||||
{
|
* Return a property of the response if it exists.
|
||||||
$this->code = $code;
|
* Possibilities include: code, raw_body, headers, body (if the response is json-decodable)
|
||||||
$this->headers = $this->get_headers_from_curl_response($headers);
|
* @return mixed
|
||||||
$this->raw_body = $raw_body;
|
*/
|
||||||
$this->body = $raw_body;
|
public function __get($property)
|
||||||
$json = json_decode($raw_body);
|
{
|
||||||
|
if (property_exists($this, $property)) {
|
||||||
if (json_last_error() == JSON_ERROR_NONE) {
|
return $this->$property;
|
||||||
$this->body = $json;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the properties of this object
|
||||||
|
* @param string $property the property name
|
||||||
|
* @param mixed $value the property value
|
||||||
|
*/
|
||||||
|
public function __set($property, $value)
|
||||||
|
{
|
||||||
|
if (property_exists($this, $property)) {
|
||||||
|
$this->$property = $value;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the cURL response headers from the
|
||||||
|
* header string and convert it into an array
|
||||||
|
* @param string $headers header string from cURL response
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function get_headers_from_curl_response($headers)
|
||||||
|
{
|
||||||
|
$headers = explode("\r\n", $headers);
|
||||||
|
array_shift($headers);
|
||||||
|
|
||||||
|
foreach ($headers as $line) {
|
||||||
|
if (strstr($line, ': ')) {
|
||||||
|
list($key, $value) = explode(': ', $line);
|
||||||
|
$result[$key] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return $result;
|
||||||
* Return a property of the response if it exists.
|
}
|
||||||
* Possibilities include: code, raw_body, headers, body (if the response is json-decodable)
|
|
||||||
* @return mixed
|
}
|
||||||
*/
|
|
||||||
public function __get($property)
|
|
||||||
{
|
|
||||||
if (property_exists($this, $property)) {
|
|
||||||
return $this->$property;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the properties of this object
|
|
||||||
* @param string $property the property name
|
|
||||||
* @param mixed $value the property value
|
|
||||||
*/
|
|
||||||
public function __set($property, $value)
|
|
||||||
{
|
|
||||||
if (property_exists($this, $property)) {
|
|
||||||
$this->$property = $value;
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the cURL response headers from the
|
|
||||||
* header string and convert it into an array
|
|
||||||
* @param string $headers header string from cURL response
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function get_headers_from_curl_response($headers)
|
|
||||||
{
|
|
||||||
$headers = explode("\r\n", $headers);
|
|
||||||
array_shift($headers);
|
|
||||||
|
|
||||||
foreach ($headers as $line) {
|
|
||||||
if (strstr($line, ': ')) {
|
|
||||||
list ($key, $value) = explode(': ', $line);
|
|
||||||
$result[$key] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,309 +1,319 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Unirest\HttpMethod;
|
use Unirest\HttpMethod;
|
||||||
use Unirest\HttpResponse;
|
use Unirest\HttpResponse;
|
||||||
|
|
||||||
class Unirest
|
|
||||||
{
|
|
||||||
|
|
||||||
private static $verifyPeer = true;
|
|
||||||
|
|
||||||
private static $socketTimeout = null;
|
|
||||||
private static $defaultHeaders = array();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Verify SSL peer
|
|
||||||
* @param bool $enabled enable SSL verification, by default is true
|
|
||||||
*/
|
|
||||||
public static function verifyPeer($enabled) {
|
|
||||||
Unirest::$verifyPeer = $enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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(), $parameters = NULL, $username = NULL, $password = NULL)
|
|
||||||
{
|
|
||||||
return Unirest::request(HttpMethod::GET, $url, $parameters, $headers, $username, $password);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send POST request to a URL
|
|
||||||
* @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, $username = NULL, $password = NULL)
|
|
||||||
{
|
|
||||||
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(), $body = NULL, $username = NULL, $password = NULL)
|
|
||||||
{
|
|
||||||
return Unirest::request(HttpMethod::DELETE, $url, $body, $headers, $username, $password);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send PUT request to a URL
|
|
||||||
* @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, $username = NULL, $password = NULL)
|
|
||||||
{
|
|
||||||
return Unirest::request(HttpMethod::PUT, $url, $body, $headers, $username, $password);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send PATCH request to a URL
|
|
||||||
* @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, $username = NULL, $password = NULL)
|
|
||||||
{
|
|
||||||
return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prepares a file for upload. To be used inside the parameters declaration for a request.
|
|
||||||
* @param string $path The file path
|
|
||||||
*/
|
|
||||||
public static function file($path) {
|
|
||||||
if (function_exists("curl_file_create")) {
|
|
||||||
return curl_file_create($path);
|
|
||||||
} else {
|
|
||||||
return "@" . $path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function is useful for serializing multidimensional arrays, and avoid getting
|
|
||||||
* the "Array to string conversion" notice
|
|
||||||
*/
|
|
||||||
private static function http_build_query_for_curl( $arrays, &$new = array(), $prefix = null ) {
|
|
||||||
if ( is_object( $arrays ) ) {
|
|
||||||
$arrays = get_object_vars( $arrays );
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ( $arrays AS $key => $value ) {
|
|
||||||
$k = isset( $prefix ) ? $prefix . '[' . $key . ']' : $key;
|
|
||||||
if ( is_array( $value ) OR is_object( $value ) ) {
|
|
||||||
Unirest::http_build_query_for_curl( $value, $new, $k );
|
|
||||||
} else {
|
|
||||||
$new[$k] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send a cURL request
|
|
||||||
* @param string $httpMethod HTTP method to use (based off \Unirest\HttpMethod constants)
|
|
||||||
* @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 HttpResponse
|
|
||||||
*/
|
|
||||||
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) {
|
|
||||||
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
if( is_array($body) || $body instanceof Traversable ) {
|
|
||||||
Unirest::http_build_query_for_curl($body, $postBody);
|
|
||||||
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postBody);
|
|
||||||
} else {
|
|
||||||
curl_setopt ($ch, CURLOPT_POSTFIELDS, $body);
|
|
||||||
}
|
|
||||||
} else if (is_array($body)) {
|
|
||||||
if (strpos($url,'?') !== false) {
|
|
||||||
$url .= "&";
|
|
||||||
} else {
|
|
||||||
$url .= "?";
|
|
||||||
}
|
|
||||||
Unirest::http_build_query_for_curl($body, $postBody);
|
|
||||||
$url .= http_build_query($postBody);
|
|
||||||
}
|
|
||||||
|
|
||||||
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, Unirest::$verifyPeer);
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Split the full response in its headers and body
|
|
||||||
$curl_info = curl_getinfo($ch);
|
|
||||||
$header_size = $curl_info["header_size"];
|
|
||||||
$header = substr($response, 0, $header_size);
|
|
||||||
$body = substr($response, $header_size);
|
|
||||||
$httpCode = $curl_info["http_code"];
|
|
||||||
|
|
||||||
return new HttpResponse($httpCode, $body, $header);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function getArrayFromQuerystring($querystring) {
|
|
||||||
$pairs = explode("&", $querystring);
|
|
||||||
$vars = array();
|
|
||||||
foreach ($pairs as $pair) {
|
|
||||||
$nv = explode("=", $pair);
|
|
||||||
$name = $nv[0];
|
|
||||||
$value = $nv[1];
|
|
||||||
$vars[$name] = $value;
|
|
||||||
}
|
|
||||||
return $vars;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ensure that a URL is encoded and safe to use with cURL
|
|
||||||
* @param string $url URL to encode
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private static function encodeUrl($url)
|
|
||||||
{
|
|
||||||
$url_parsed = parse_url($url);
|
|
||||||
|
|
||||||
$scheme = $url_parsed['scheme'] . '://';
|
|
||||||
$host = $url_parsed['host'];
|
|
||||||
$port = (isset($url_parsed['port']) ? $url_parsed['port'] : null);
|
|
||||||
$path = (isset($url_parsed['path']) ? $url_parsed['path'] : null);
|
|
||||||
$query = (isset($url_parsed['query']) ? $url_parsed['query'] : null);
|
|
||||||
|
|
||||||
if ($query != null) {
|
|
||||||
$query = '?' . http_build_query(Unirest::getArrayFromQuerystring($url_parsed['query']));
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($port && $port[0] != ":")
|
|
||||||
$port = ":" . $port;
|
|
||||||
|
|
||||||
$result = $scheme . $host . $port . $path . $query;
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function getHeader($key, $val) {
|
|
||||||
$key = trim(strtolower($key));
|
|
||||||
if ($key == "user-agent" || $key == "expect") continue;
|
|
||||||
return $key . ": " . $val;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!function_exists('http_chunked_decode')) {
|
|
||||||
/**
|
|
||||||
* Dechunk an http 'transfer-encoding: chunked' message
|
|
||||||
* @param string $chunk the encoded message
|
|
||||||
* @return string the decoded message
|
|
||||||
*/
|
|
||||||
function http_chunked_decode($chunk)
|
|
||||||
{
|
|
||||||
$pos = 0;
|
|
||||||
$len = strlen($chunk);
|
|
||||||
$dechunk = null;
|
|
||||||
|
|
||||||
while (($pos < $len)
|
|
||||||
&& ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) {
|
|
||||||
|
|
||||||
if (!is_hex($chunkLenHex)) {
|
|
||||||
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
|
|
||||||
return $chunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
$pos = $newlineAt + 1;
|
|
||||||
$chunkLen = hexdec(rtrim($chunkLenHex, "\r\n"));
|
|
||||||
$dechunk .= substr($chunk, $pos, $chunkLen);
|
|
||||||
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $dechunk;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
class Unirest
|
||||||
|
{
|
||||||
|
|
||||||
|
private static $verifyPeer = true;
|
||||||
|
private static $socketTimeout = null;
|
||||||
|
private static $defaultHeaders = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* determine if a string can represent a number in hexadecimal
|
* Verify SSL peer
|
||||||
* @link http://uk1.php.net/ctype_xdigit
|
* @param bool $enabled enable SSL verification, by default is true
|
||||||
* @param string $hex
|
|
||||||
* @return boolean true if the string is a hex, otherwise false
|
|
||||||
*/
|
*/
|
||||||
function is_hex($hex)
|
public static function verifyPeer($enabled)
|
||||||
{
|
{
|
||||||
return ctype_xdigit($hex);
|
Unirest::$verifyPeer = $enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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(), $parameters = NULL, $username = NULL, $password = NULL)
|
||||||
|
{
|
||||||
|
return Unirest::request(HttpMethod::GET, $url, $parameters, $headers, $username, $password);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send POST request to a URL
|
||||||
|
* @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, $username = NULL, $password = NULL)
|
||||||
|
{
|
||||||
|
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(), $body = NULL, $username = NULL, $password = NULL)
|
||||||
|
{
|
||||||
|
return Unirest::request(HttpMethod::DELETE, $url, $body, $headers, $username, $password);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send PUT request to a URL
|
||||||
|
* @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, $username = NULL, $password = NULL)
|
||||||
|
{
|
||||||
|
return Unirest::request(HttpMethod::PUT, $url, $body, $headers, $username, $password);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send PATCH request to a URL
|
||||||
|
* @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, $username = NULL, $password = NULL)
|
||||||
|
{
|
||||||
|
return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepares a file for upload. To be used inside the parameters declaration for a request.
|
||||||
|
* @param string $path The file path
|
||||||
|
*/
|
||||||
|
public static function file($path)
|
||||||
|
{
|
||||||
|
if (function_exists("curl_file_create")) {
|
||||||
|
return curl_file_create($path);
|
||||||
|
} else {
|
||||||
|
return "@" . $path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function is useful for serializing multidimensional arrays, and avoid getting
|
||||||
|
* the "Array to string conversion" notice
|
||||||
|
*/
|
||||||
|
private static function http_build_query_for_curl($arrays, &$new = array(), $prefix = null)
|
||||||
|
{
|
||||||
|
if (is_object($arrays)) {
|
||||||
|
$arrays = get_object_vars($arrays);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($arrays AS $key => $value) {
|
||||||
|
$k = isset($prefix) ? $prefix . '[' . $key . ']' : $key;
|
||||||
|
if (is_array($value) OR is_object($value)) {
|
||||||
|
Unirest::http_build_query_for_curl($value, $new, $k);
|
||||||
|
} else {
|
||||||
|
$new[$k] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a cURL request
|
||||||
|
* @param string $httpMethod HTTP method to use (based off \Unirest\HttpMethod constants)
|
||||||
|
* @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 HttpResponse
|
||||||
|
*/
|
||||||
|
private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL)
|
||||||
|
{
|
||||||
|
if ($headers == NULL)
|
||||||
|
$headers = array();
|
||||||
|
|
||||||
|
$lowercaseHeaders = array();
|
||||||
|
$finalHeaders = array_merge($headers, Unirest::$defaultHeaders);
|
||||||
|
foreach ($finalHeaders as $key => $val) {
|
||||||
|
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
||||||
|
}
|
||||||
|
|
||||||
|
$lowerCaseFinalHeaders = array_change_key_case($finalHeaders);
|
||||||
|
if (!array_key_exists("user-agent", $lowerCaseFinalHeaders)) {
|
||||||
|
$lowercaseHeaders[] = "user-agent: unirest-php/1.1";
|
||||||
|
}
|
||||||
|
if (!array_key_exists("expect", $lowerCaseFinalHeaders)) {
|
||||||
|
$lowercaseHeaders[] = "expect:";
|
||||||
|
}
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
if ($httpMethod != HttpMethod::GET) {
|
||||||
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
||||||
|
if (is_array($body) || $body instanceof Traversable) {
|
||||||
|
Unirest::http_build_query_for_curl($body, $postBody);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody);
|
||||||
|
} else {
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
||||||
|
}
|
||||||
|
} else if (is_array($body)) {
|
||||||
|
if (strpos($url, '?') !== false) {
|
||||||
|
$url .= "&";
|
||||||
|
} else {
|
||||||
|
$url .= "?";
|
||||||
|
}
|
||||||
|
Unirest::http_build_query_for_curl($body, $postBody);
|
||||||
|
$url .= urldecode(http_build_query($postBody));
|
||||||
|
}
|
||||||
|
|
||||||
|
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, Unirest::$verifyPeer);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split the full response in its headers and body
|
||||||
|
$curl_info = curl_getinfo($ch);
|
||||||
|
$header_size = $curl_info["header_size"];
|
||||||
|
$header = substr($response, 0, $header_size);
|
||||||
|
$body = substr($response, $header_size);
|
||||||
|
$httpCode = $curl_info["http_code"];
|
||||||
|
|
||||||
|
return new HttpResponse($httpCode, $body, $header);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function getArrayFromQuerystring($querystring)
|
||||||
|
{
|
||||||
|
$pairs = explode("&", $querystring);
|
||||||
|
$vars = array();
|
||||||
|
foreach ($pairs as $pair) {
|
||||||
|
$nv = explode("=", $pair, 2);
|
||||||
|
$name = $nv[0];
|
||||||
|
$value = $nv[1];
|
||||||
|
$vars[$name] = $value;
|
||||||
|
}
|
||||||
|
return $vars;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure that a URL is encoded and safe to use with cURL
|
||||||
|
* @param string $url URL to encode
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private static function encodeUrl($url)
|
||||||
|
{
|
||||||
|
$url_parsed = parse_url($url);
|
||||||
|
|
||||||
|
$scheme = $url_parsed['scheme'] . '://';
|
||||||
|
$host = $url_parsed['host'];
|
||||||
|
$port = (isset($url_parsed['port']) ? $url_parsed['port'] : null);
|
||||||
|
$path = (isset($url_parsed['path']) ? $url_parsed['path'] : null);
|
||||||
|
$query = (isset($url_parsed['query']) ? $url_parsed['query'] : null);
|
||||||
|
|
||||||
|
if ($query != null) {
|
||||||
|
$query = '?' . http_build_query(Unirest::getArrayFromQuerystring($url_parsed['query']));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($port && $port[0] != ":")
|
||||||
|
$port = ":" . $port;
|
||||||
|
|
||||||
|
$result = $scheme . $host . $port . $path . $query;
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function getHeader($key, $val)
|
||||||
|
{
|
||||||
|
$key = trim(strtolower($key));
|
||||||
|
return $key . ": " . $val;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!function_exists('http_chunked_decode')) {
|
||||||
|
/**
|
||||||
|
* Dechunk an http 'transfer-encoding: chunked' message
|
||||||
|
* @param string $chunk the encoded message
|
||||||
|
* @return string the decoded message
|
||||||
|
*/
|
||||||
|
function http_chunked_decode($chunk)
|
||||||
|
{
|
||||||
|
$pos = 0;
|
||||||
|
$len = strlen($chunk);
|
||||||
|
$dechunk = null;
|
||||||
|
|
||||||
|
while (($pos < $len) && ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) {
|
||||||
|
|
||||||
|
if (!is_hex($chunkLenHex)) {
|
||||||
|
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
|
||||||
|
return $chunk;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pos = $newlineAt + 1;
|
||||||
|
$chunkLen = hexdec(rtrim($chunkLenHex, "\r\n"));
|
||||||
|
$dechunk .= substr($chunk, $pos, $chunkLen);
|
||||||
|
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $dechunk;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* determine if a string can represent a number in hexadecimal
|
||||||
|
* @link http://uk1.php.net/ctype_xdigit
|
||||||
|
* @param string $hex
|
||||||
|
* @return boolean true if the string is a hex, otherwise false
|
||||||
|
*/
|
||||||
|
function is_hex($hex)
|
||||||
|
{
|
||||||
|
return ctype_xdigit($hex);
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
@@ -1,23 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
echo "Running the Unirest-PHP bindings test suite.\n".
|
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";
|
||||||
"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');
|
$ok = @include_once(dirname(__FILE__) . '/simpletest/autorun.php');
|
||||||
if (!$ok) {
|
if (!$ok) {
|
||||||
$ok = @include_once(dirname(__FILE__).'/../vendor/vierbergenlars/simpletest/autorun.php');
|
$ok = @include_once(dirname(__FILE__) . '/../vendor/vierbergenlars/simpletest/autorun.php');
|
||||||
}
|
}
|
||||||
if (!$ok) {
|
if (!$ok) {
|
||||||
echo "MISSING DEPENDENCY: The Unirest-PHP test cases depend on SimpleTest. ".
|
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";
|
||||||
"Download it at <http://www.simpletest.org/>, and either install it ".
|
exit(1);
|
||||||
"in your PHP include_path or put it in the test/ directory.\n";
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Throw an exception on any error
|
// Throw an exception on any error
|
||||||
function exception_error_handler($errno, $errstr, $errfile, $errline) {
|
function exception_error_handler($errno, $errstr, $errfile, $errline)
|
||||||
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
|
{
|
||||||
|
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
|
||||||
}
|
}
|
||||||
|
|
||||||
set_error_handler('exception_error_handler');
|
set_error_handler('exception_error_handler');
|
||||||
|
|||||||
@@ -2,242 +2,334 @@
|
|||||||
|
|
||||||
class UnirestTest extends UnitTestCase
|
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);
|
public function testGet()
|
||||||
|
{
|
||||||
$args = $response->body->args;
|
$response = Unirest::get("http://httpbin.org/get?name=Mark", array(
|
||||||
$this->assertEqual("Mark", $args->name);
|
"Accept" => "application/json"
|
||||||
$this->assertEqual("thefosk", $args->nick);
|
), array(
|
||||||
}
|
"nick" => "thefosk"
|
||||||
|
));
|
||||||
public function testGetMultidimensionalArray()
|
|
||||||
{
|
$this->assertEqual(200, $response->code);
|
||||||
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ),
|
|
||||||
array('key'=>'value','items'=>array('item1','item2')));
|
$args = $response->body->args;
|
||||||
|
$this->assertEqual("Mark", $args->name);
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEqual("thefosk", $args->nick);
|
||||||
|
}
|
||||||
$args = $response->body->args;
|
|
||||||
|
|
||||||
$this->assertEqual("value", $args->key);
|
|
||||||
$this->assertEqual("item1", $args->{"items%5B0%5D"});
|
|
||||||
$this->assertEqual("item2", $args->{"items%5B1%5D"});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetWithDots()
|
|
||||||
{
|
|
||||||
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ),
|
|
||||||
array(
|
|
||||||
"user.name" => "Mark",
|
|
||||||
"nick" => "thefosk"
|
|
||||||
));
|
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
|
||||||
|
|
||||||
$args = $response->body->args;
|
|
||||||
$this->assertEqual("Mark", $args->{"user.name"});
|
|
||||||
$this->assertEqual("thefosk", $args->nick);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetWithDots2()
|
|
||||||
{
|
|
||||||
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ),
|
|
||||||
array(
|
|
||||||
"user.name" => "Mark Bond",
|
|
||||||
"nick" => "thefosk"
|
|
||||||
));
|
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
|
||||||
|
|
||||||
$args = $response->body->args;
|
public function testGetMultidimensionalArray()
|
||||||
$this->assertEqual("Mark+Bond", $args->{"user.name"});
|
{
|
||||||
$this->assertEqual("thefosk", $args->nick);
|
$response = Unirest::get("http://httpbin.org/get", array(
|
||||||
}
|
"Accept" => "application/json"
|
||||||
|
), array(
|
||||||
|
'key' => 'value',
|
||||||
|
'items' => array(
|
||||||
|
'item1',
|
||||||
|
'item2'
|
||||||
|
)
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
$args = $response->body->args;
|
||||||
|
|
||||||
|
$this->assertEqual("value", $args->key);
|
||||||
|
$this->assertEqual("item1", $args->{"items[0]"});
|
||||||
|
$this->assertEqual("item2", $args->{"items[1]"});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetWithDots()
|
||||||
|
{
|
||||||
|
$response = Unirest::get("http://httpbin.org/get", array(
|
||||||
|
"Accept" => "application/json"
|
||||||
|
), array(
|
||||||
|
"user.name" => "Mark",
|
||||||
|
"nick" => "thefosk"
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
$args = $response->body->args;
|
||||||
|
$this->assertEqual("Mark", $args->{"user.name"});
|
||||||
|
$this->assertEqual("thefosk", $args->nick);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetWithDots2()
|
||||||
|
{
|
||||||
|
$response = Unirest::get("http://httpbin.org/get", array(
|
||||||
|
"Accept" => "application/json"
|
||||||
|
), array(
|
||||||
|
"user.name" => "Mark Bond",
|
||||||
|
"nick" => "thefosk"
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
$args = $response->body->args;
|
||||||
|
$this->assertEqual("Mark Bond", $args->{"user.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 testPost()
|
public function testPostWithEqualSign()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
|
$response = Unirest::post("http://httpbin.org/post", array(
|
||||||
array(
|
"Accept" => "application/json"
|
||||||
"name" => "Mark",
|
), array(
|
||||||
"nick" => "thefosk"
|
"name" => "Mark=Hello"
|
||||||
));
|
));
|
||||||
|
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
$form = $response->body->form;
|
||||||
|
$this->assertEqual("Mark=Hello", $form->name);
|
||||||
|
}
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
public function testGetWithEqualSign()
|
||||||
|
{
|
||||||
|
$response = Unirest::get("http://httpbin.org/get", array(
|
||||||
|
"Accept" => "application/json"
|
||||||
|
), array(
|
||||||
|
"name" => "Mark=Hello"
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
$args = $response->body->args;
|
||||||
|
$this->assertEqual("Mark=Hello", $args->name);
|
||||||
|
}
|
||||||
|
|
||||||
$form = $response->body->form;
|
public function testPostArray()
|
||||||
$this->assertEqual("Mark", $form->name);
|
{
|
||||||
$this->assertEqual("thefosk", $form->nick);
|
$response = Unirest::post("http://httpbin.org/post", array(
|
||||||
}
|
"Accept" => "application/json"
|
||||||
|
), array(
|
||||||
|
"name[0]" => "Mark",
|
||||||
|
"name[1]" => "John"
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
$form = $response->body->form;
|
||||||
|
|
||||||
public function testPostWithDots()
|
$this->assertEqual("Mark", $form->{"name[0]"});
|
||||||
{
|
$this->assertEqual("John", $form->{"name[1]"});
|
||||||
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
|
}
|
||||||
array(
|
|
||||||
"user.name" => "Mark",
|
|
||||||
"nick" => "thefosk"
|
|
||||||
));
|
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
|
||||||
|
|
||||||
$form = $response->body->form;
|
|
||||||
$this->assertEqual("Mark", $form->{"user.name"});
|
|
||||||
$this->assertEqual("thefosk", $form->nick);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public function testGetArray()
|
||||||
|
{
|
||||||
|
$response = Unirest::get("http://httpbin.org/get", array(), array(
|
||||||
|
"name[0]" => "Mark",
|
||||||
|
"name[1]" => "John"
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
$args = $response->body->args;
|
||||||
|
$this->assertEqual("Mark", $args->{"name[0]"});
|
||||||
|
$this->assertEqual("John", $args->{"name[1]"});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPostWithDots()
|
||||||
|
{
|
||||||
|
$response = Unirest::post("http://httpbin.org/post", array(
|
||||||
|
"Accept" => "application/json"
|
||||||
|
), array(
|
||||||
|
"user.name" => "Mark",
|
||||||
|
"nick" => "thefosk"
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
$form = $response->body->form;
|
||||||
|
$this->assertEqual("Mark", $form->{"user.name"});
|
||||||
|
$this->assertEqual("thefosk", $form->nick);
|
||||||
|
}
|
||||||
|
|
||||||
public function testRawPost()
|
public function testRawPost()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
|
$response = Unirest::post("http://httpbin.org/post", array(
|
||||||
json_encode(array(
|
"Accept" => "application/json"
|
||||||
"author" => "Sam Sullivan"
|
), json_encode(array(
|
||||||
)));
|
"author" => "Sam Sullivan"
|
||||||
|
)));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
$json = $response->body->json;
|
$json = $response->body->json;
|
||||||
$this->assertEqual("Sam Sullivan", $json->author);
|
$this->assertEqual("Sam Sullivan", $json->author);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testUpload()
|
||||||
|
{
|
||||||
|
$response = Unirest::post("http://httpbin.org/post", array(
|
||||||
|
"Accept" => "application/json"
|
||||||
|
), array(
|
||||||
|
"name" => "Mark",
|
||||||
|
"file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
|
||||||
|
));
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
$files = $response->body->files;
|
||||||
|
$this->assertEqual("This is a test", $files->file);
|
||||||
|
|
||||||
|
$form = $response->body->form;
|
||||||
|
$this->assertEqual("Mark", $form->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPostMultidimensionalArray()
|
||||||
|
{
|
||||||
|
$response = Unirest::post("http://httpbin.org/post", array(
|
||||||
|
"Accept" => "application/json"
|
||||||
|
), array(
|
||||||
|
'key' => 'value',
|
||||||
|
'items' => array(
|
||||||
|
'item1',
|
||||||
|
'item2'
|
||||||
|
)
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
$form = $response->body->form;
|
||||||
|
$this->assertEqual("value", $form->key);
|
||||||
|
$this->assertEqual("item1", $form->{"items[0]"});
|
||||||
|
$this->assertEqual("item2", $form->{"items[1]"});
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
Unirest::timeout(null); // Cleaning timeout for the other tests
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTimeoutSuccess()
|
||||||
|
{
|
||||||
|
Unirest::timeout(3);
|
||||||
|
|
||||||
|
$response = Unirest::get("http://httpbin.org/delay/1");
|
||||||
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
|
Unirest::timeout(null); // Cleaning timeout for the other tests
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
public function testUpload() {
|
public function testCustomHeaders()
|
||||||
|
{
|
||||||
|
$response = Unirest::get('http://httpbin.org/get', array(
|
||||||
|
'user-agent' => 'ciao',
|
||||||
|
));
|
||||||
|
|
||||||
var_dump(file_get_contents(dirname(__FILE__) . "/test_upload.txt"));
|
$this->assertEqual(200, $response->code);
|
||||||
|
|
||||||
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
|
|
||||||
array(
|
|
||||||
"name" => "Mark",
|
|
||||||
"file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->assertEqual(200, $response->code);
|
|
||||||
|
|
||||||
$files = $response->body->files;
|
|
||||||
var_dump($response->body);
|
|
||||||
var_dump($files);
|
|
||||||
$this->assertEqual("This is a test", $files->file);
|
|
||||||
|
|
||||||
$form = $response->body->form;
|
|
||||||
$this->assertEqual("Mark", $form->name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testPostMultidimensionalArray()
|
|
||||||
{
|
|
||||||
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
|
|
||||||
array('key'=>'value','items'=>array('item1','item2')));
|
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
|
||||||
|
|
||||||
$form = $response->body->form;
|
|
||||||
$this->assertEqual("value", $form->key);
|
|
||||||
$this->assertEqual("item1", $form->{"items[0]"});
|
|
||||||
$this->assertEqual("item2", $form->{"items[1]"});
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
Unirest::timeout(null); // Cleaning timeout for the other tests
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testTimeoutSuccess()
|
|
||||||
{
|
|
||||||
Unirest::timeout(3);
|
|
||||||
|
|
||||||
$response = Unirest::get("http://httpbin.org/delay/1");
|
|
||||||
$this->assertEqual(200, $response->code);
|
|
||||||
|
|
||||||
Unirest::timeout(null); // Cleaning timeout for the other tests
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$headers = $response->body->headers;
|
||||||
|
$this->assertEqual("ciao", $headers->{'User-Agent'});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user