pass additional params to request method to ask for associative json objects
This commit is contained in:
@@ -5,11 +5,11 @@ use Unirest\HttpResponse;
|
|||||||
|
|
||||||
class Unirest
|
class Unirest
|
||||||
{
|
{
|
||||||
|
|
||||||
private static $verifyPeer = true;
|
private static $verifyPeer = true;
|
||||||
private static $socketTimeout = null;
|
private static $socketTimeout = null;
|
||||||
private static $defaultHeaders = array();
|
private static $defaultHeaders = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verify SSL peer
|
* Verify SSL peer
|
||||||
* @param bool $enabled enable SSL verification, by default is true
|
* @param bool $enabled enable SSL verification, by default is true
|
||||||
@@ -18,7 +18,7 @@ class Unirest
|
|||||||
{
|
{
|
||||||
Unirest::$verifyPeer = $enabled;
|
Unirest::$verifyPeer = $enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a timeout
|
* Set a timeout
|
||||||
* @param integer $seconds timeout value in seconds
|
* @param integer $seconds timeout value in seconds
|
||||||
@@ -27,7 +27,7 @@ class Unirest
|
|||||||
{
|
{
|
||||||
Unirest::$socketTimeout = $seconds;
|
Unirest::$socketTimeout = $seconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a new default header to send on every request
|
* Set a new default header to send on every request
|
||||||
* @param string $name header name
|
* @param string $name header name
|
||||||
@@ -37,7 +37,7 @@ class Unirest
|
|||||||
{
|
{
|
||||||
Unirest::$defaultHeaders[$name] = $value;
|
Unirest::$defaultHeaders[$name] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear all the default headers
|
* Clear all the default headers
|
||||||
*/
|
*/
|
||||||
@@ -45,7 +45,7 @@ class Unirest
|
|||||||
{
|
{
|
||||||
Unirest::$defaultHeaders = array();
|
Unirest::$defaultHeaders = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a GET request to a URL
|
* Send a GET request to a URL
|
||||||
* @param string $url URL to send the GET request to
|
* @param string $url URL to send the GET request to
|
||||||
@@ -59,7 +59,7 @@ class Unirest
|
|||||||
{
|
{
|
||||||
return Unirest::request(HttpMethod::GET, $url, $parameters, $headers, $username, $password);
|
return Unirest::request(HttpMethod::GET, $url, $parameters, $headers, $username, $password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send POST request to a URL
|
* Send POST request to a URL
|
||||||
* @param string $url URL to send the POST request to
|
* @param string $url URL to send the POST request to
|
||||||
@@ -73,7 +73,7 @@ class Unirest
|
|||||||
{
|
{
|
||||||
return Unirest::request(HttpMethod::POST, $url, $body, $headers, $username, $password);
|
return Unirest::request(HttpMethod::POST, $url, $body, $headers, $username, $password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send DELETE request to a URL
|
* Send DELETE request to a URL
|
||||||
* @param string $url URL to send the DELETE request to
|
* @param string $url URL to send the DELETE request to
|
||||||
@@ -87,7 +87,7 @@ class Unirest
|
|||||||
{
|
{
|
||||||
return Unirest::request(HttpMethod::DELETE, $url, $body, $headers, $username, $password);
|
return Unirest::request(HttpMethod::DELETE, $url, $body, $headers, $username, $password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send PUT request to a URL
|
* Send PUT request to a URL
|
||||||
* @param string $url URL to send the PUT request to
|
* @param string $url URL to send the PUT request to
|
||||||
@@ -101,7 +101,7 @@ class Unirest
|
|||||||
{
|
{
|
||||||
return Unirest::request(HttpMethod::PUT, $url, $body, $headers, $username, $password);
|
return Unirest::request(HttpMethod::PUT, $url, $body, $headers, $username, $password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send PATCH request to a URL
|
* Send PATCH request to a URL
|
||||||
* @param string $url URL to send the PATCH request to
|
* @param string $url URL to send the PATCH request to
|
||||||
@@ -115,7 +115,7 @@ class Unirest
|
|||||||
{
|
{
|
||||||
return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password);
|
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.
|
* Prepares a file for upload. To be used inside the parameters declaration for a request.
|
||||||
* @param string $path The file path
|
* @param string $path The file path
|
||||||
@@ -128,7 +128,7 @@ class Unirest
|
|||||||
return "@" . $path;
|
return "@" . $path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function is useful for serializing multidimensional arrays, and avoid getting
|
* This function is useful for serializing multidimensional arrays, and avoid getting
|
||||||
* the "Array to string conversion" notice
|
* the "Array to string conversion" notice
|
||||||
@@ -138,7 +138,7 @@ class Unirest
|
|||||||
if (is_object($arrays)) {
|
if (is_object($arrays)) {
|
||||||
$arrays = get_object_vars($arrays);
|
$arrays = get_object_vars($arrays);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($arrays AS $key => $value) {
|
foreach ($arrays AS $key => $value) {
|
||||||
$k = isset($prefix) ? $prefix . '[' . $key . ']' : $key;
|
$k = isset($prefix) ? $prefix . '[' . $key . ']' : $key;
|
||||||
if (!$value instanceof \CURLFile AND (is_array($value) OR is_object($value))) {
|
if (!$value instanceof \CURLFile AND (is_array($value) OR is_object($value))) {
|
||||||
@@ -148,7 +148,7 @@ class Unirest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a cURL request
|
* Send a cURL request
|
||||||
* @param string $httpMethod HTTP method to use (based off \Unirest\HttpMethod constants)
|
* @param string $httpMethod HTTP method to use (based off \Unirest\HttpMethod constants)
|
||||||
@@ -160,7 +160,7 @@ class Unirest
|
|||||||
* @throws Exception if a cURL error occurs
|
* @throws Exception if a cURL error occurs
|
||||||
* @return HttpResponse
|
* @return HttpResponse
|
||||||
*/
|
*/
|
||||||
private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL)
|
private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL, $json_decode_assoc = false)
|
||||||
{
|
{
|
||||||
if ($headers == NULL)
|
if ($headers == NULL)
|
||||||
$headers = array();
|
$headers = array();
|
||||||
@@ -170,7 +170,7 @@ class Unirest
|
|||||||
foreach ($finalHeaders as $key => $val) {
|
foreach ($finalHeaders as $key => $val) {
|
||||||
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
||||||
}
|
}
|
||||||
|
|
||||||
$lowerCaseFinalHeaders = array_change_key_case($finalHeaders);
|
$lowerCaseFinalHeaders = array_change_key_case($finalHeaders);
|
||||||
if (!array_key_exists("user-agent", $lowerCaseFinalHeaders)) {
|
if (!array_key_exists("user-agent", $lowerCaseFinalHeaders)) {
|
||||||
$lowercaseHeaders[] = "user-agent: unirest-php/1.1";
|
$lowercaseHeaders[] = "user-agent: unirest-php/1.1";
|
||||||
@@ -178,7 +178,7 @@ class Unirest
|
|||||||
if (!array_key_exists("expect", $lowerCaseFinalHeaders)) {
|
if (!array_key_exists("expect", $lowerCaseFinalHeaders)) {
|
||||||
$lowercaseHeaders[] = "expect:";
|
$lowercaseHeaders[] = "expect:";
|
||||||
}
|
}
|
||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
if ($httpMethod != HttpMethod::GET) {
|
if ($httpMethod != HttpMethod::GET) {
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
||||||
@@ -197,7 +197,7 @@ class Unirest
|
|||||||
Unirest::http_build_query_for_curl($body, $postBody);
|
Unirest::http_build_query_for_curl($body, $postBody);
|
||||||
$url .= urldecode(http_build_query($postBody));
|
$url .= urldecode(http_build_query($postBody));
|
||||||
}
|
}
|
||||||
|
|
||||||
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_RETURNTRANSFER, true);
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||||
@@ -212,23 +212,23 @@ class Unirest
|
|||||||
if (!empty($username)) {
|
if (!empty($username)) {
|
||||||
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . ((empty($password)) ? "" : $password));
|
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . ((empty($password)) ? "" : $password));
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
$error = curl_error($ch);
|
$error = curl_error($ch);
|
||||||
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);
|
$curl_info = curl_getinfo($ch);
|
||||||
$header_size = $curl_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 = $curl_info["http_code"];
|
||||||
|
|
||||||
return new HttpResponse($httpCode, $body, $header);
|
return new HttpResponse($httpCode, $body, $header, $json_decode_assoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getArrayFromQuerystring($querystring)
|
private static function getArrayFromQuerystring($querystring)
|
||||||
{
|
{
|
||||||
$pairs = explode("&", $querystring);
|
$pairs = explode("&", $querystring);
|
||||||
@@ -241,7 +241,7 @@ class Unirest
|
|||||||
}
|
}
|
||||||
return $vars;
|
return $vars;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure that a URL is encoded and safe to use with cURL
|
* Ensure that a URL is encoded and safe to use with cURL
|
||||||
* @param string $url URL to encode
|
* @param string $url URL to encode
|
||||||
@@ -250,36 +250,36 @@ class Unirest
|
|||||||
private static function encodeUrl($url)
|
private static function encodeUrl($url)
|
||||||
{
|
{
|
||||||
$url_parsed = parse_url($url);
|
$url_parsed = parse_url($url);
|
||||||
|
|
||||||
$scheme = $url_parsed['scheme'] . '://';
|
$scheme = $url_parsed['scheme'] . '://';
|
||||||
$host = $url_parsed['host'];
|
$host = $url_parsed['host'];
|
||||||
$port = (isset($url_parsed['port']) ? $url_parsed['port'] : null);
|
$port = (isset($url_parsed['port']) ? $url_parsed['port'] : null);
|
||||||
$path = (isset($url_parsed['path']) ? $url_parsed['path'] : null);
|
$path = (isset($url_parsed['path']) ? $url_parsed['path'] : null);
|
||||||
$query = (isset($url_parsed['query']) ? $url_parsed['query'] : null);
|
$query = (isset($url_parsed['query']) ? $url_parsed['query'] : null);
|
||||||
|
|
||||||
if ($query != null) {
|
if ($query != null) {
|
||||||
$query = '?' . http_build_query(Unirest::getArrayFromQuerystring($url_parsed['query']));
|
$query = '?' . http_build_query(Unirest::getArrayFromQuerystring($url_parsed['query']));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($port && $port[0] != ":")
|
if ($port && $port[0] != ":")
|
||||||
$port = ":" . $port;
|
$port = ":" . $port;
|
||||||
|
|
||||||
$result = $scheme . $host . $port . $path . $query;
|
$result = $scheme . $host . $port . $path . $query;
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getHeader($key, $val)
|
private static function getHeader($key, $val)
|
||||||
{
|
{
|
||||||
$key = trim(strtolower($key));
|
$key = trim(strtolower($key));
|
||||||
return $key . ": " . $val;
|
return $key . ": " . $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!function_exists('http_chunked_decode')) {
|
if (!function_exists('http_chunked_decode')) {
|
||||||
/**
|
/**
|
||||||
* Dechunk an http 'transfer-encoding: chunked' message
|
* Dechunk an http 'transfer-encoding: chunked' message
|
||||||
* @param string $chunk the encoded message
|
* @param string $chunk the encoded message
|
||||||
* @return string the decoded message
|
* @return string the decoded message
|
||||||
*/
|
*/
|
||||||
function http_chunked_decode($chunk)
|
function http_chunked_decode($chunk)
|
||||||
@@ -287,29 +287,29 @@ if (!function_exists('http_chunked_decode')) {
|
|||||||
$pos = 0;
|
$pos = 0;
|
||||||
$len = strlen($chunk);
|
$len = strlen($chunk);
|
||||||
$dechunk = null;
|
$dechunk = null;
|
||||||
|
|
||||||
while (($pos < $len) && ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) {
|
while (($pos < $len) && ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) {
|
||||||
|
|
||||||
if (!is_hex($chunkLenHex)) {
|
if (!is_hex($chunkLenHex)) {
|
||||||
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
|
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
|
||||||
return $chunk;
|
return $chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
$pos = $newlineAt + 1;
|
$pos = $newlineAt + 1;
|
||||||
$chunkLen = hexdec(rtrim($chunkLenHex, "\r\n"));
|
$chunkLen = hexdec(rtrim($chunkLenHex, "\r\n"));
|
||||||
$dechunk .= substr($chunk, $pos, $chunkLen);
|
$dechunk .= substr($chunk, $pos, $chunkLen);
|
||||||
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
|
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dechunk;
|
return $dechunk;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* determine if a string can represent a number in hexadecimal
|
* determine if a string can represent a number in hexadecimal
|
||||||
* @link http://uk1.php.net/ctype_xdigit
|
* @link http://uk1.php.net/ctype_xdigit
|
||||||
* @param string $hex
|
* @param string $hex
|
||||||
* @return boolean true if the string is a hex, otherwise false
|
* @return boolean true if the string is a hex, otherwise false
|
||||||
*/
|
*/
|
||||||
function is_hex($hex)
|
function is_hex($hex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user