Bugfixes
This commit is contained in:
@@ -1,9 +1,7 @@
|
|||||||
Mashape PHP Client
|
Unicorn-PHP
|
||||||
============================================
|
============================================
|
||||||
|
|
||||||
This is the base library for Mashape client libraries.
|
Unicorn is a set of lightweight HTTP libraries available in PHP, Ruby, Python, Java, Objective-C.
|
||||||
|
|
||||||
For questions or feedback visit [mashape.com](http://mashape.com/)
|
|
||||||
|
|
||||||
Documentation
|
Documentation
|
||||||
-------------------
|
-------------------
|
||||||
|
|||||||
@@ -4,73 +4,55 @@ require_once(dirname(__FILE__) . "/Chunked.php");
|
|||||||
require_once(dirname(__FILE__) . "/HttpResponse.php");
|
require_once(dirname(__FILE__) . "/HttpResponse.php");
|
||||||
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
||||||
|
|
||||||
class HttpClient {
|
class Unicorn {
|
||||||
|
|
||||||
const USER_AGENT = "mashape-php/3.0";
|
|
||||||
|
|
||||||
public static function get($url, $headers = array()) {
|
public static function get($url, $headers = array()) {
|
||||||
return HttpClient::request(HttpMethod::GET, $url, NULL, $headers);
|
return Unicorn::request(HttpMethod::GET, $url, NULL, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function post($url, $body = NULL, $headers = array()) {
|
public static function post($url, $headers = array(), $body = NULL) {
|
||||||
return HttpClient::request(HttpMethod::POST, $url, $body, $headers);
|
return Unicorn::request(HttpMethod::POST, $url, $body, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function delete($url, $headers = array(), $body = NULL) {
|
||||||
|
return Unicorn::request(HttpMethod::DELETE, $url, $body, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function put($url, $headers = array(), $body = NULL) {
|
||||||
|
return Unicorn::request(HttpMethod::PUT, $url, $body, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function patch($url, $headers = array(), $body = NULL) {
|
||||||
|
return Unicorn::request(HttpMethod::PATCH, $url, $body, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function request($httpMethod, $url, $body = NULL, $headers = array()) {
|
private static function request($httpMethod, $url, $body = NULL, $headers = array()) {
|
||||||
|
|
||||||
$lowercaseHeaders = array();
|
$lowercaseHeaders = array();
|
||||||
foreach ($headers as $key => $val) {
|
foreach ($headers as $key => $val) {
|
||||||
$lowercaseHeaders[strtolower($key)] = $val;
|
$key = trim(strtolower($key));
|
||||||
|
if ($key == "user-agent") continue;
|
||||||
|
$lowercaseHeaders[] = $key . ": " . $val;
|
||||||
}
|
}
|
||||||
|
$lowercaseHeaders[] = "user-agent: unicorn-php/1.0";
|
||||||
$lowercaseHeaders["user-agent"] = USER_AGENT;
|
|
||||||
|
|
||||||
$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);
|
||||||
//TODO: Remove
|
|
||||||
/*
|
|
||||||
if (is_array($body)) {
|
|
||||||
$parameters = "";
|
|
||||||
foreach($body as $key => $value) {
|
|
||||||
if (is_array($value)) {
|
|
||||||
throw new Exception("Nested arrays are not supported");
|
|
||||||
}
|
|
||||||
|
|
||||||
$parameters .= $key . "=";
|
|
||||||
if (substr($value, 0, 1) == "@") {
|
|
||||||
// It's a path
|
|
||||||
$parameters .= $value;
|
|
||||||
} else {
|
|
||||||
$parameters .= rawurlencode($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
$parameters .= "&";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strlen($parameters) > 1) {
|
|
||||||
$parameters = substr($parameters, 0, strlen($parameters) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
$body = $parameters;
|
|
||||||
var_dump($body);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
curl_setopt ($ch, CURLOPT_POSTFIELDS, $body);
|
curl_setopt ($ch, CURLOPT_POSTFIELDS, $body);
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_setopt ($ch, CURLOPT_URL , HttpClient::encodeUrl($url));
|
curl_setopt ($ch, CURLOPT_URL , Unicorn::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, array('Expect:'));
|
|
||||||
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, false);
|
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
if (curl_error($ch)) {
|
$error = curl_error($ch);
|
||||||
throw new Exception(curl_error($ch));
|
if ($error) {
|
||||||
|
throw new Exception($error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split the full response in its headers and body
|
// Split the full response in its headers and body
|
||||||
@@ -83,8 +65,6 @@ class HttpClient {
|
|||||||
return new HttpResponse($httpCode, $body, $header);
|
return new HttpResponse($httpCode, $body, $header);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static function encodeUrl($url) {
|
private static function encodeUrl($url) {
|
||||||
$parsedUrl = parse_url($url);
|
$parsedUrl = parse_url($url);
|
||||||
parse_str( $parsedUrl['query'], $query ); // generating an array by reference (yes, kinda weird)
|
parse_str( $parsedUrl['query'], $query ); // generating an array by reference (yes, kinda weird)
|
||||||
Reference in New Issue
Block a user