Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -66,12 +66,12 @@ $response->raw_body; // Unparsed body
|
||||
|
||||
### File Uploads
|
||||
|
||||
To upload files in a multipart form representation simply place an `@` symbol before the path:
|
||||
To upload files in a multipart form representation use the return value of `Unirest::file($path)` as the value of a parameter:
|
||||
|
||||
```php
|
||||
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
|
||||
array(
|
||||
"file" => "@/tmp/file.txt"
|
||||
"file" => Unirest::file("/tmp/file.txt") // Tells Unirest where the file is located
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
@@ -12,4 +12,3 @@
|
||||
const PATCH = "PATCH";
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
{
|
||||
|
||||
private static $verifyPeer = true;
|
||||
|
||||
private static $socketTimeout = null;
|
||||
private static $defaultHeaders = array();
|
||||
|
||||
@@ -15,7 +14,8 @@
|
||||
* Verify SSL peer
|
||||
* @param bool $enabled enable SSL verification, by default is true
|
||||
*/
|
||||
public static function verifyPeer($enabled) {
|
||||
public static function verifyPeer($enabled)
|
||||
{
|
||||
Unirest::$verifyPeer = $enabled;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
* Set a timeout
|
||||
* @param integer $seconds timeout value in seconds
|
||||
*/
|
||||
public static function timeout($seconds) {
|
||||
public static function timeout($seconds)
|
||||
{
|
||||
Unirest::$socketTimeout = $seconds;
|
||||
}
|
||||
|
||||
@@ -32,14 +33,16 @@
|
||||
* @param string $name header name
|
||||
* @param string $value header value
|
||||
*/
|
||||
public static function defaultHeader($name, $value) {
|
||||
public static function defaultHeader($name, $value)
|
||||
{
|
||||
Unirest::$defaultHeaders[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all the default headers
|
||||
*/
|
||||
public static function clearDefaultHeaders() {
|
||||
public static function clearDefaultHeaders()
|
||||
{
|
||||
Unirest::$defaultHeaders = array();
|
||||
}
|
||||
|
||||
@@ -113,6 +116,39 @@
|
||||
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)
|
||||
@@ -126,33 +162,40 @@
|
||||
*/
|
||||
private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL)
|
||||
{
|
||||
if ($headers == NULL) $headers = array();
|
||||
if ($headers == NULL)
|
||||
$headers = array();
|
||||
|
||||
$lowercaseHeaders = array();
|
||||
foreach ($headers as $key => $val) {
|
||||
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
||||
}
|
||||
foreach (Unirest::$defaultHeaders as $key => $val) {
|
||||
$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 .= "?";
|
||||
}
|
||||
|
||||
foreach ($body as $parameter => $val) {
|
||||
$url .= $parameter . "=" . $val . "&";
|
||||
}
|
||||
$url = substr($url, 0, strlen($url) - 1);
|
||||
Unirest::http_build_query_for_curl($body, $postBody);
|
||||
$url .= urldecode(http_build_query($postBody));
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, Unirest::encodeUrl($url));
|
||||
@@ -186,6 +229,19 @@
|
||||
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
|
||||
@@ -202,8 +258,7 @@
|
||||
$query = (isset($url_parsed['query']) ? $url_parsed['query'] : null);
|
||||
|
||||
if ($query != null) {
|
||||
parse_str($url_parsed['query'], $query_parsed);
|
||||
$query = '?' . http_build_query($query_parsed);
|
||||
$query = '?' . http_build_query(Unirest::getArrayFromQuerystring($url_parsed['query']));
|
||||
}
|
||||
|
||||
if ($port && $port[0] != ":")
|
||||
@@ -213,9 +268,9 @@
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function getHeader($key, $val) {
|
||||
private static function getHeader($key, $val)
|
||||
{
|
||||
$key = trim(strtolower($key));
|
||||
if ($key == "user-agent" || $key == "expect") continue;
|
||||
return $key . ": " . $val;
|
||||
}
|
||||
|
||||
@@ -233,8 +288,7 @@
|
||||
$len = strlen($chunk);
|
||||
$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)) {
|
||||
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
|
||||
|
||||
@@ -1,22 +1,19 @@
|
||||
<?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";
|
||||
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";
|
||||
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) {
|
||||
function exception_error_handler($errno, $errstr, $errfile, $errline)
|
||||
{
|
||||
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
class UnirestTest extends UnitTestCase
|
||||
{
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$response = Unirest::get("http://httpbin.org/get?name=Mark", array( "Accept" => "application/json" ),
|
||||
array(
|
||||
$response = Unirest::get("http://httpbin.org/get?name=Mark", array(
|
||||
"Accept" => "application/json"
|
||||
), array(
|
||||
"nick" => "thefosk"
|
||||
));
|
||||
|
||||
@@ -16,10 +18,64 @@ class UnirestTest extends UnitTestCase
|
||||
$this->assertEqual("thefosk", $args->nick);
|
||||
}
|
||||
|
||||
public function testGetMultidimensionalArray()
|
||||
{
|
||||
$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(
|
||||
$response = Unirest::post("http://httpbin.org/post", array(
|
||||
"Accept" => "application/json"
|
||||
), array(
|
||||
"name" => "Mark",
|
||||
"nick" => "thefosk"
|
||||
));
|
||||
@@ -31,10 +87,148 @@ class UnirestTest extends UnitTestCase
|
||||
$this->assertEqual("thefosk", $form->nick);
|
||||
}
|
||||
|
||||
public function testPostWithEqualSign()
|
||||
{
|
||||
$response = Unirest::post("http://httpbin.org/post", array(
|
||||
"Accept" => "application/json"
|
||||
), array(
|
||||
"name" => "Mark=Hello"
|
||||
));
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
|
||||
$form = $response->body->form;
|
||||
$this->assertEqual("Mark=Hello", $form->name);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
$response = Unirest::get("http://httpbin.org/get", array(
|
||||
"Accept" => "application/json"
|
||||
), array(
|
||||
"name" => "Mark=Hello=John"
|
||||
));
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
|
||||
$args = $response->body->args;
|
||||
$this->assertEqual("Mark=Hello=John", $args->name);
|
||||
}
|
||||
|
||||
public function testPostArray()
|
||||
{
|
||||
$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;
|
||||
|
||||
$this->assertEqual("Mark", $form->{"name[0]"});
|
||||
$this->assertEqual("John", $form->{"name[1]"});
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
$response = Unirest::post("http://httpbin.org/post", array(
|
||||
"Accept" => "application/json"
|
||||
), json_encode(array(
|
||||
"author" => "Sam Sullivan"
|
||||
)));
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
|
||||
$json = $response->body->json;
|
||||
$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(
|
||||
$response = Unirest::put("http://httpbin.org/put", array(
|
||||
"Accept" => "application/json"
|
||||
), array(
|
||||
"name" => "Mark",
|
||||
"nick" => "thefosk"
|
||||
));
|
||||
@@ -48,8 +242,9 @@ class UnirestTest extends UnitTestCase
|
||||
|
||||
public function testPatch()
|
||||
{
|
||||
$response = Unirest::patch("http://httpbin.org/patch", array( "Accept" => "application/json" ),
|
||||
array(
|
||||
$response = Unirest::patch("http://httpbin.org/patch", array(
|
||||
"Accept" => "application/json"
|
||||
), array(
|
||||
"name" => "Mark",
|
||||
"nick" => "thefosk"
|
||||
));
|
||||
@@ -63,8 +258,10 @@ class UnirestTest extends UnitTestCase
|
||||
|
||||
public function testDelete()
|
||||
{
|
||||
$response = Unirest::delete("http://httpbin.org/delete", array( "Accept" => "application/json", "Content-Type" => "application/x-www-form-urlencoded" ),
|
||||
array(
|
||||
$response = Unirest::delete("http://httpbin.org/delete", array(
|
||||
"Accept" => "application/json",
|
||||
"Content-Type" => "application/x-www-form-urlencoded"
|
||||
), array(
|
||||
"name" => "Mark",
|
||||
"nick" => "thefosk"
|
||||
));
|
||||
@@ -134,4 +331,16 @@ class UnirestTest extends UnitTestCase
|
||||
$this->assertEqual("Basic dXNlcjpwYXNzd29yZA==", $headers->Authorization);
|
||||
}
|
||||
|
||||
public function testCustomHeaders()
|
||||
{
|
||||
$response = Unirest::get('http://httpbin.org/get', array(
|
||||
'user-agent' => 'ciao',
|
||||
));
|
||||
|
||||
$this->assertEqual(200, $response->code);
|
||||
|
||||
$headers = $response->body->headers;
|
||||
$this->assertEqual("ciao", $headers->{'User-Agent'});
|
||||
}
|
||||
|
||||
}
|
||||
1
test/Unirest/test_upload.txt
Normal file
1
test/Unirest/test_upload.txt
Normal file
@@ -0,0 +1 @@
|
||||
This is a test
|
||||
Reference in New Issue
Block a user