formatting

This commit is contained in:
thefosk
2014-03-18 11:15:38 -07:00
parent e6682a5ad1
commit 2fb13154e2
5 changed files with 654 additions and 625 deletions

View File

@@ -1,9 +1,9 @@
<?php <?php
namespace Unirest; namespace Unirest;
interface HttpMethod interface HttpMethod
{ {
const DELETE = "DELETE"; const DELETE = "DELETE";
const GET = "GET"; const GET = "GET";
@@ -11,5 +11,4 @@
const PUT = "PUT"; const PUT = "PUT";
const PATCH = "PATCH"; const PATCH = "PATCH";
} }

View File

@@ -1,9 +1,9 @@
<?php <?php
namespace Unirest; namespace Unirest;
class HttpResponse class HttpResponse
{ {
private $code; private $code;
private $raw_body; private $raw_body;
@@ -66,7 +66,7 @@
foreach ($headers as $line) { foreach ($headers as $line) {
if (strstr($line, ': ')) { if (strstr($line, ': ')) {
list ($key, $value) = explode(': ', $line); list($key, $value) = explode(': ', $line);
$result[$key] = $value; $result[$key] = $value;
} }
} }
@@ -74,4 +74,4 @@
return $result; return $result;
} }
} }

View File

@@ -1,13 +1,12 @@
<?php <?php
use Unirest\HttpMethod; use Unirest\HttpMethod;
use Unirest\HttpResponse; 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();
@@ -15,7 +14,8 @@
* 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
*/ */
public static function verifyPeer($enabled) { public static function verifyPeer($enabled)
{
Unirest::$verifyPeer = $enabled; Unirest::$verifyPeer = $enabled;
} }
@@ -23,7 +23,8 @@
* Set a timeout * Set a timeout
* @param integer $seconds timeout value in seconds * @param integer $seconds timeout value in seconds
*/ */
public static function timeout($seconds) { public static function timeout($seconds)
{
Unirest::$socketTimeout = $seconds; Unirest::$socketTimeout = $seconds;
} }
@@ -32,14 +33,16 @@
* @param string $name header name * @param string $name header name
* @param string $value header value * @param string $value header value
*/ */
public static function defaultHeader($name, $value) { public static function defaultHeader($name, $value)
{
Unirest::$defaultHeaders[$name] = $value; Unirest::$defaultHeaders[$name] = $value;
} }
/** /**
* Clear all the default headers * Clear all the default headers
*/ */
public static function clearDefaultHeaders() { public static function clearDefaultHeaders()
{
Unirest::$defaultHeaders = array(); Unirest::$defaultHeaders = array();
} }
@@ -117,7 +120,8 @@
* 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
*/ */
public static function file($path) { public static function file($path)
{
if (function_exists("curl_file_create")) { if (function_exists("curl_file_create")) {
return curl_file_create($path); return curl_file_create($path);
} else { } else {
@@ -129,15 +133,16 @@
* 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
*/ */
private static function http_build_query_for_curl( $arrays, &$new = array(), $prefix = null ) { private static function http_build_query_for_curl($arrays, &$new = array(), $prefix = null)
if ( is_object( $arrays ) ) { {
$arrays = get_object_vars( $arrays ); if (is_object($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 ( is_array( $value ) OR is_object( $value ) ) { if (is_array($value) OR is_object($value)) {
Unirest::http_build_query_for_curl( $value, $new, $k ); Unirest::http_build_query_for_curl($value, $new, $k);
} else { } else {
$new[$k] = $value; $new[$k] = $value;
} }
@@ -157,7 +162,8 @@
*/ */
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)
{ {
if ($headers == NULL) $headers = array(); if ($headers == NULL)
$headers = array();
$lowercaseHeaders = array(); $lowercaseHeaders = array();
foreach ($headers as $key => $val) { foreach ($headers as $key => $val) {
$lowercaseHeaders[] = Unirest::getHeader($key, $val); $lowercaseHeaders[] = Unirest::getHeader($key, $val);
@@ -171,15 +177,15 @@
$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);
if( is_array($body) || $body instanceof Traversable ) { if (is_array($body) || $body instanceof Traversable) {
Unirest::http_build_query_for_curl($body, $postBody); Unirest::http_build_query_for_curl($body, $postBody);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postBody); curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody);
} else { } else {
curl_setopt ($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
} }
} else if (is_array($body)) { } else if (is_array($body)) {
if (strpos($url,'?') !== false) { if (strpos($url, '?') !== false) {
$url .= "&"; $url .= "&";
} else { } else {
$url .= "?"; $url .= "?";
@@ -188,16 +194,16 @@
$url .= http_build_query($postBody); $url .= 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);
curl_setopt ($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders); curl_setopt($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders);
curl_setopt ($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, Unirest::$verifyPeer); 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. curl_setopt($ch, CURLOPT_ENCODING, ""); // If an empty string, "", is set, a header containing all supported encoding types is sent.
if (Unirest::$socketTimeout != null) { if (Unirest::$socketTimeout != null) {
curl_setopt ($ch, CURLOPT_TIMEOUT, Unirest::$socketTimeout); curl_setopt($ch, CURLOPT_TIMEOUT, Unirest::$socketTimeout);
} }
if (!empty($username)) { if (!empty($username)) {
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . ((empty($password)) ? "" : $password)); curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . ((empty($password)) ? "" : $password));
@@ -219,7 +225,8 @@
return new HttpResponse($httpCode, $body, $header); return new HttpResponse($httpCode, $body, $header);
} }
private static function getArrayFromQuerystring($querystring) { private static function getArrayFromQuerystring($querystring)
{
$pairs = explode("&", $querystring); $pairs = explode("&", $querystring);
$vars = array(); $vars = array();
foreach ($pairs as $pair) { foreach ($pairs as $pair) {
@@ -257,15 +264,17 @@
return $result; return $result;
} }
private static function getHeader($key, $val) { private static function getHeader($key, $val)
{
$key = trim(strtolower($key)); $key = trim(strtolower($key));
if ($key == "user-agent" || $key == "expect") continue; if ($key == "user-agent" || $key == "expect")
continue;
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
@@ -277,8 +286,7 @@
$len = strlen($chunk); $len = strlen($chunk);
$dechunk = null; $dechunk = null;
while (($pos < $len) while (($pos < $len) && ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) {
&& ($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);
@@ -293,17 +301,17 @@
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)
{ {
return ctype_xdigit($hex); return ctype_xdigit($hex);
} }
?> ?>

View File

@@ -1,22 +1,19 @@
<?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 ".
"in your PHP include_path or put it in the test/ directory.\n";
exit(1); 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);
} }

View File

@@ -4,8 +4,9 @@ class UnirestTest extends UnitTestCase
{ {
public function testGet() public function testGet()
{ {
$response = Unirest::get("http://httpbin.org/get?name=Mark", array( "Accept" => "application/json" ), $response = Unirest::get("http://httpbin.org/get?name=Mark", array(
array( "Accept" => "application/json"
), array(
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -18,8 +19,15 @@ class UnirestTest extends UnitTestCase
public function testGetMultidimensionalArray() public function testGetMultidimensionalArray()
{ {
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), $response = Unirest::get("http://httpbin.org/get", array(
array('key'=>'value','items'=>array('item1','item2'))); "Accept" => "application/json"
), array(
'key' => 'value',
'items' => array(
'item1',
'item2'
)
));
$this->assertEqual(200, $response->code); $this->assertEqual(200, $response->code);
@@ -32,8 +40,9 @@ class UnirestTest extends UnitTestCase
public function testGetWithDots() public function testGetWithDots()
{ {
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), $response = Unirest::get("http://httpbin.org/get", array(
array( "Accept" => "application/json"
), array(
"user.name" => "Mark", "user.name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -47,8 +56,9 @@ class UnirestTest extends UnitTestCase
public function testGetWithDots2() public function testGetWithDots2()
{ {
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), $response = Unirest::get("http://httpbin.org/get", array(
array( "Accept" => "application/json"
), array(
"user.name" => "Mark Bond", "user.name" => "Mark Bond",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -62,8 +72,9 @@ class UnirestTest extends UnitTestCase
public function testPost() public function testPost()
{ {
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), $response = Unirest::post("http://httpbin.org/post", array(
array( "Accept" => "application/json"
), array(
"name" => "Mark", "name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -77,8 +88,9 @@ class UnirestTest extends UnitTestCase
public function testPostWithDots() public function testPostWithDots()
{ {
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), $response = Unirest::post("http://httpbin.org/post", array(
array( "Accept" => "application/json"
), array(
"user.name" => "Mark", "user.name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -92,8 +104,9 @@ class UnirestTest extends UnitTestCase
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"
), json_encode(array(
"author" => "Sam Sullivan" "author" => "Sam Sullivan"
))); )));
@@ -103,16 +116,17 @@ class UnirestTest extends UnitTestCase
$this->assertEqual("Sam Sullivan", $json->author); $this->assertEqual("Sam Sullivan", $json->author);
} }
public function testUpload() { public function testUpload()
{
var_dump(file_get_contents(dirname(__FILE__) . "/test_upload.txt")); var_dump(file_get_contents(dirname(__FILE__) . "/test_upload.txt"));
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), $response = Unirest::post("http://httpbin.org/post", array(
array( "Accept" => "application/json"
), array(
"name" => "Mark", "name" => "Mark",
"file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt") "file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
) ));
);
$this->assertEqual(200, $response->code); $this->assertEqual(200, $response->code);
$files = $response->body->files; $files = $response->body->files;
@@ -126,8 +140,15 @@ class UnirestTest extends UnitTestCase
public function testPostMultidimensionalArray() public function testPostMultidimensionalArray()
{ {
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), $response = Unirest::post("http://httpbin.org/post", array(
array('key'=>'value','items'=>array('item1','item2'))); "Accept" => "application/json"
), array(
'key' => 'value',
'items' => array(
'item1',
'item2'
)
));
$this->assertEqual(200, $response->code); $this->assertEqual(200, $response->code);
@@ -139,8 +160,9 @@ class UnirestTest extends UnitTestCase
public function testPut() public function testPut()
{ {
$response = Unirest::put("http://httpbin.org/put", array( "Accept" => "application/json" ), $response = Unirest::put("http://httpbin.org/put", array(
array( "Accept" => "application/json"
), array(
"name" => "Mark", "name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -154,8 +176,9 @@ class UnirestTest extends UnitTestCase
public function testPatch() public function testPatch()
{ {
$response = Unirest::patch("http://httpbin.org/patch", array( "Accept" => "application/json" ), $response = Unirest::patch("http://httpbin.org/patch", array(
array( "Accept" => "application/json"
), array(
"name" => "Mark", "name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -169,8 +192,10 @@ class UnirestTest extends UnitTestCase
public function testDelete() public function testDelete()
{ {
$response = Unirest::delete("http://httpbin.org/delete", array( "Accept" => "application/json", "Content-Type" => "application/x-www-form-urlencoded" ), $response = Unirest::delete("http://httpbin.org/delete", array(
array( "Accept" => "application/json",
"Content-Type" => "application/x-www-form-urlencoded"
), array(
"name" => "Mark", "name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));