From 2bd214f2d9bf37d1cad763ed55682d79f72addfc Mon Sep 17 00:00:00 2001 From: Ahmad Nassri Date: Wed, 17 Dec 2014 18:54:05 -0500 Subject: [PATCH] pass additional params to request method to ask for associative json objects --- lib/Unirest/Unirest.php | 78 ++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index 8cb109e..ff96d7f 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -5,11 +5,11 @@ 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 @@ -18,7 +18,7 @@ class Unirest { Unirest::$verifyPeer = $enabled; } - + /** * Set a timeout * @param integer $seconds timeout value in seconds @@ -27,7 +27,7 @@ class Unirest { Unirest::$socketTimeout = $seconds; } - + /** * Set a new default header to send on every request * @param string $name header name @@ -37,7 +37,7 @@ class Unirest { Unirest::$defaultHeaders[$name] = $value; } - + /** * Clear all the default headers */ @@ -45,7 +45,7 @@ class Unirest { Unirest::$defaultHeaders = array(); } - + /** * Send a GET request to a URL * @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); } - + /** * Send POST request to a URL * @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); } - + /** * Send DELETE request to a URL * @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); } - + /** * Send PUT request to a URL * @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); } - + /** * Send PATCH request to a URL * @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); } - + /** * Prepares a file for upload. To be used inside the parameters declaration for a request. * @param string $path The file path @@ -128,7 +128,7 @@ class Unirest return "@" . $path; } } - + /** * This function is useful for serializing multidimensional arrays, and avoid getting * the "Array to string conversion" notice @@ -138,7 +138,7 @@ class Unirest if (is_object($arrays)) { $arrays = get_object_vars($arrays); } - + foreach ($arrays AS $key => $value) { $k = isset($prefix) ? $prefix . '[' . $key . ']' : $key; if (!$value instanceof \CURLFile AND (is_array($value) OR is_object($value))) { @@ -148,7 +148,7 @@ class Unirest } } } - + /** * Send a cURL request * @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 * @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) $headers = array(); @@ -170,7 +170,7 @@ class Unirest 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"; @@ -178,7 +178,7 @@ class Unirest if (!array_key_exists("expect", $lowerCaseFinalHeaders)) { $lowercaseHeaders[] = "expect:"; } - + $ch = curl_init(); if ($httpMethod != HttpMethod::GET) { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); @@ -197,7 +197,7 @@ class Unirest 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); @@ -212,23 +212,23 @@ class Unirest 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); + + return new HttpResponse($httpCode, $body, $header, $json_decode_assoc); } - + private static function getArrayFromQuerystring($querystring) { $pairs = explode("&", $querystring); @@ -241,7 +241,7 @@ class Unirest } return $vars; } - + /** * Ensure that a URL is encoded and safe to use with cURL * @param string $url URL to encode @@ -250,36 +250,36 @@ class Unirest 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 + * Dechunk an http 'transfer-encoding: chunked' message + * @param string $chunk the encoded message * @return string the decoded message */ function http_chunked_decode($chunk) @@ -287,29 +287,29 @@ if (!function_exists('http_chunked_decode')) { $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 + * 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 + * @param string $hex + * @return boolean true if the string is a hex, otherwise false */ function is_hex($hex) {