resolving conflicts; indenting
This commit is contained in:
38
README.md
38
README.md
@@ -4,39 +4,35 @@ Unirest is a set of lightweight HTTP libraries available in multiple languages.
|
|||||||
|
|
||||||
Created with love by http://mashape.com
|
Created with love by http://mashape.com
|
||||||
|
|
||||||
|
### Install with Composer
|
||||||
|
If you're using [Composer](https://github.com/composer/composer) to manage
|
||||||
### Installing
|
dependencies, you can add Unirest with it.
|
||||||
Unirest-PHP requires PHP `v5.3+`. Download the PHP library from Github, and require in your script like so:
|
|
||||||
|
|
||||||
```php
|
|
||||||
require_once './lib/Unirest.php';
|
|
||||||
```
|
|
||||||
|
|
||||||
### Using Composer
|
|
||||||
|
|
||||||
[Composer](http://getcomposer.org/) is a package manager for PHP.
|
|
||||||
|
|
||||||
In the composer.json file in your project add:
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
{
|
{
|
||||||
"require" : {
|
"require" : {
|
||||||
"mashape/unirest-php" : "dev-master"
|
"mashape/unirest-php" : "dev-master"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-0": {"Unirest": "lib/"}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
And then run:
|
|
||||||
|
|
||||||
```
|
### Install source from GitHub
|
||||||
php composer.phar install
|
Unirest-PHP requires PHP `v5.3+`. Download the PHP library from Github, and require in your script like so:
|
||||||
|
|
||||||
|
To install the source code:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ git clone git@github.com:Mashape/unirest-php.git
|
||||||
```
|
```
|
||||||
|
|
||||||
Include the library in your project with:
|
And include it in your scripts:
|
||||||
|
|
||||||
```php
|
```bash
|
||||||
require 'vendor/autoload.php';
|
require_once '/path/to/unirest-php/lib/Unirest.php';
|
||||||
````
|
```
|
||||||
|
|
||||||
## Creating Request
|
## Creating Request
|
||||||
So you're probably wondering how using Unirest makes creating requests in PHP easier, let's look at a working example:
|
So you're probably wondering how using Unirest makes creating requests in PHP easier, let's look at a working example:
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
<?php namespace Unirest;
|
<?php
|
||||||
class HttpMethod
|
|
||||||
{
|
namespace Unirest;
|
||||||
const DELETE = "DELETE";
|
|
||||||
const GET = "GET";
|
interface HttpMethod
|
||||||
const POST = "POST";
|
{
|
||||||
const PUT = "PUT";
|
|
||||||
const PATCH = "PATCH";
|
const DELETE = "DELETE";
|
||||||
}
|
const GET = "GET";
|
||||||
|
const POST = "POST";
|
||||||
|
const PUT = "PUT";
|
||||||
|
const PATCH = "PATCH";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,77 +1,77 @@
|
|||||||
<?php namespace Unirest;
|
<?php
|
||||||
class HttpResponse
|
|
||||||
{
|
|
||||||
private $code;
|
|
||||||
private $raw_body;
|
|
||||||
private $body;
|
|
||||||
private $headers;
|
|
||||||
|
|
||||||
/**
|
namespace Unirest;
|
||||||
* HttpResponse constructor
|
|
||||||
* @param int $code Response code of the cURL request
|
|
||||||
* @param string $raw_body The raw body of the cURL response
|
|
||||||
* @param string $headers Raw header string from cURL response
|
|
||||||
*/
|
|
||||||
function __construct($code, $raw_body, $headers)
|
|
||||||
{
|
|
||||||
$this->code = $code;
|
|
||||||
$this->headers = $this->get_headers_from_curl_response($headers);
|
|
||||||
$this->raw_body = $raw_body;
|
|
||||||
$this->body = $raw_body;
|
|
||||||
$json = json_decode($raw_body);
|
|
||||||
if (json_last_error() == JSON_ERROR_NONE) {
|
|
||||||
$this->body = $json;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
class HttpResponse
|
||||||
* Return a property of the response if it exists
|
{
|
||||||
* Possibilities include:
|
|
||||||
* - code
|
|
||||||
* - raw_body
|
|
||||||
* - body (if the response is json-decodable)
|
|
||||||
* - headers
|
|
||||||
* @param [type] $property [description]
|
|
||||||
* @return [type] [description]
|
|
||||||
*/
|
|
||||||
public function __get($property)
|
|
||||||
{
|
|
||||||
if (property_exists($this, $property)) {
|
|
||||||
return $this->$property;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private $code;
|
||||||
* Set the properties of this object
|
private $raw_body;
|
||||||
* @param string $property The property name
|
private $body;
|
||||||
* @param mixed $value The property value
|
private $headers;
|
||||||
*/
|
|
||||||
public function __set($property, $value)
|
|
||||||
{
|
|
||||||
if (property_exists($this, $property)) {
|
|
||||||
$this->$property = $value;
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the cURL response headers from the
|
|
||||||
* header string and convert it into an array
|
|
||||||
* @param string $headers header string from cURL response
|
|
||||||
* @return array headers in array form
|
|
||||||
*/
|
|
||||||
private function get_headers_from_curl_response($headers)
|
|
||||||
{
|
|
||||||
$headers = explode("\r\n", $headers);
|
|
||||||
array_shift($headers);
|
|
||||||
|
|
||||||
foreach ($headers as $line) {
|
/**
|
||||||
if (strstr($line, ': ')) {
|
* @param int $code response code of the cURL request
|
||||||
list ($key, $value) = explode(': ', $line);
|
* @param string $raw_body the raw body of the cURL response
|
||||||
$result[$key] = $value;
|
* @param string $headers raw header string from cURL response
|
||||||
}
|
*/
|
||||||
}
|
public function __construct($code, $raw_body, $headers)
|
||||||
|
{
|
||||||
|
$this->code = $code;
|
||||||
|
$this->headers = $this->get_headers_from_curl_response($headers);
|
||||||
|
$this->raw_body = $raw_body;
|
||||||
|
$this->body = $raw_body;
|
||||||
|
$json = json_decode($raw_body);
|
||||||
|
|
||||||
|
if (json_last_error() == JSON_ERROR_NONE) {
|
||||||
|
$this->body = $json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $result;
|
/**
|
||||||
}
|
* Return a property of the response if it exists.
|
||||||
}
|
* Possibilities include: code, raw_body, headers, body (if the response is json-decodable)
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function __get($property)
|
||||||
|
{
|
||||||
|
if (property_exists($this, $property)) {
|
||||||
|
return $this->$property;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the properties of this object
|
||||||
|
* @param string $property the property name
|
||||||
|
* @param mixed $value the property value
|
||||||
|
*/
|
||||||
|
public function __set($property, $value)
|
||||||
|
{
|
||||||
|
if (property_exists($this, $property)) {
|
||||||
|
$this->$property = $value;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the cURL response headers from the
|
||||||
|
* header string and convert it into an array
|
||||||
|
* @param string $headers header string from cURL response
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function get_headers_from_curl_response($headers)
|
||||||
|
{
|
||||||
|
$headers = explode("\r\n", $headers);
|
||||||
|
array_shift($headers);
|
||||||
|
|
||||||
|
foreach ($headers as $line) {
|
||||||
|
if (strstr($line, ': ')) {
|
||||||
|
list ($key, $value) = explode(': ', $line);
|
||||||
|
$result[$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,262 +1,255 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Unirest\HttpMethod;
|
use Unirest\HttpMethod;
|
||||||
use Unirest\HttpResponse;
|
use Unirest\HttpResponse;
|
||||||
|
|
||||||
class Unirest
|
class Unirest
|
||||||
{
|
{
|
||||||
|
|
||||||
private static $socketTimeout = null;
|
private static $socketTimeout = null;
|
||||||
private static $defaultHeaders = array();
|
private static $defaultHeaders = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
* @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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @param mixed $parameters Parameters to send in the querystring
|
* @param mixed $parameters parameters to send in the querystring
|
||||||
* @param string $username Basic Authentication username
|
* @param string $username Basic Authentication username
|
||||||
* @param string $password Basic Authentication password
|
* @param string $password Basic Authentication password
|
||||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
* @return string|stdObj response string or stdObj if response is json-decodable
|
||||||
*/
|
*/
|
||||||
public static function get($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL)
|
public static function get($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL)
|
||||||
{
|
{
|
||||||
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
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @param mixed $body POST body data
|
* @param mixed $body POST body data
|
||||||
* @param string $username Basic Authentication username
|
* @param string $username Basic Authentication username
|
||||||
* @param string $password Basic Authentication password
|
* @param string $password Basic Authentication password
|
||||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
* @return string|stdObj response string or stdObj if response is json-decodable
|
||||||
*/
|
*/
|
||||||
public static function post($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
public static function post($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
||||||
{
|
{
|
||||||
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
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @param mixed $body DELETE body data
|
* @param mixed $body DELETE body data
|
||||||
* @param string $username Basic Authentication username
|
* @param string $username Basic Authentication username
|
||||||
* @param string $password Basic Authentication password
|
* @param string $password Basic Authentication password
|
||||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
* @return string|stdObj response string or stdObj if response is json-decodable
|
||||||
*/
|
*/
|
||||||
public static function delete($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
public static function delete($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
||||||
{
|
{
|
||||||
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
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @param mixed $body PUT body data
|
* @param mixed $body PUT body data
|
||||||
* @param string $username Basic Authentication username
|
* @param string $username Basic Authentication username
|
||||||
* @param string $password Basic Authentication password
|
* @param string $password Basic Authentication password
|
||||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
* @return string|stdObj response string or stdObj if response is json-decodable
|
||||||
*/
|
*/
|
||||||
public static function put($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
public static function put($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
||||||
{
|
{
|
||||||
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
|
||||||
* @param array $headers Additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @param mixed $body PATCH body data
|
* @param mixed $body PATCH body data
|
||||||
* @param string $username Basic Authentication username
|
* @param string $username Basic Authentication username
|
||||||
* @param string $password Basic Authentication password
|
* @param string $password Basic Authentication password
|
||||||
* @return string|stdObj Response string or stdObj if response is json-decodable
|
* @return string|stdObj response string or stdObj if response is json-decodable
|
||||||
*/
|
*/
|
||||||
public static function patch($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
public static function patch($url, $headers = array(), $body = NULL, $username = NULL, $password = NULL)
|
||||||
{
|
{
|
||||||
return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password);
|
return Unirest::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getHeader($key, $val) {
|
/**
|
||||||
$key = trim(strtolower($key));
|
* Send a cURL request
|
||||||
if ($key == "user-agent" || $key == "expect") continue;
|
* @param string $httpMethod HTTP method to use (based off \Unirest\HttpMethod constants)
|
||||||
return $key . ": " . $val;
|
* @param string $url URL to send the request to
|
||||||
}
|
* @param mixed $body request body
|
||||||
|
* @param array $headers additional headers to send
|
||||||
|
* @param string $username Basic Authentication username
|
||||||
|
* @param string $password Basic Authentication password
|
||||||
|
* @throws Exception if a cURL error occurs
|
||||||
|
* @return HttpResponse
|
||||||
|
*/
|
||||||
|
private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL)
|
||||||
|
{
|
||||||
|
if ($headers == NULL) $headers = array();
|
||||||
|
$lowercaseHeaders = array();
|
||||||
|
foreach ($headers as $key => $val) {
|
||||||
|
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
||||||
|
}
|
||||||
|
foreach (Unirest::$defaultHeaders as $key => $val) {
|
||||||
|
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$lowercaseHeaders[] = "user-agent: unirest-php/1.1";
|
||||||
* Send a cURL request
|
$lowercaseHeaders[] = "expect:";
|
||||||
* @param string $httpMethod HTTP Method to use (based off \Unirest\HttpMethod constants)
|
|
||||||
* @param string $url URL to send the request to
|
|
||||||
* @param mixed $body Request body
|
|
||||||
* @param array $headers Additional headers to send
|
|
||||||
* @param string $username Basic Authentication username
|
|
||||||
* @param string $password Basic Authentication password
|
|
||||||
* @throws Exception If a cURL error occurs
|
|
||||||
* @return \Unireset\HttpResponse \Unirest\HttpResponse object
|
|
||||||
*/
|
|
||||||
private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL)
|
|
||||||
{
|
|
||||||
if ($headers == NULL) $headers = array();
|
|
||||||
$lowercaseHeaders = array();
|
|
||||||
foreach ($headers as $key => $val) {
|
|
||||||
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
|
||||||
}
|
|
||||||
foreach (Unirest::$defaultHeaders as $key => $val) {
|
|
||||||
$lowercaseHeaders[] = Unirest::getHeader($key, $val);
|
|
||||||
}
|
|
||||||
|
|
||||||
$lowercaseHeaders[] = "user-agent: unirest-php/1.1";
|
$ch = curl_init();
|
||||||
$lowercaseHeaders[] = "expect:";
|
if ($httpMethod != HttpMethod::GET) {
|
||||||
|
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_setopt ($ch, CURLOPT_URL, Unirest::encodeUrl($url));
|
||||||
|
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||||
|
curl_setopt ($ch, CURLOPT_MAXREDIRS, 10);
|
||||||
|
curl_setopt ($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders);
|
||||||
|
curl_setopt ($ch, CURLOPT_HEADER, true);
|
||||||
|
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
|
||||||
|
curl_setopt ($ch, CURLOPT_ENCODING, ""); // If an empty string, "", is set, a header containing all supported encoding types is sent.
|
||||||
|
if (Unirest::$socketTimeout != null) {
|
||||||
|
curl_setopt ($ch, CURLOPT_TIMEOUT, Unirest::$socketTimeout);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
$ch = curl_init();
|
/**
|
||||||
if ($httpMethod != HttpMethod::GET) {
|
* Ensure that a URL is encoded and safe to use with cURL
|
||||||
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
* @param string $url URL to encode
|
||||||
curl_setopt ($ch, CURLOPT_POSTFIELDS, $body);
|
* @return string
|
||||||
} else if (is_array($body)) {
|
*/
|
||||||
if (strpos($url,'?') !== false) {
|
private static function encodeUrl($url)
|
||||||
$url .= "&";
|
{
|
||||||
} else {
|
$url_parsed = parse_url($url);
|
||||||
$url .= "?";
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($body as $parameter => $val) {
|
|
||||||
$url .= $parameter . "=" . $val . "&";
|
|
||||||
}
|
|
||||||
$url = substr($url, 0, strlen($url) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
curl_setopt ($ch, CURLOPT_URL, Unirest::encodeUrl($url));
|
|
||||||
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
|
||||||
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
||||||
curl_setopt ($ch, CURLOPT_MAXREDIRS, 10);
|
|
||||||
curl_setopt ($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders);
|
|
||||||
curl_setopt ($ch, CURLOPT_HEADER, true);
|
|
||||||
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
|
|
||||||
curl_setopt ($ch, CURLOPT_ENCODING, ""); // If an empty string, "", is set, a header containing all supported encoding types is sent.
|
|
||||||
if (Unirest::$socketTimeout != null) {
|
|
||||||
curl_setopt ($ch, CURLOPT_TIMEOUT, Unirest::$socketTimeout);
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ensure that a URL is encoded and safe to use with cURL
|
|
||||||
* @param string $url URL to encode
|
|
||||||
* @return string Encoded URL
|
|
||||||
*/
|
|
||||||
private static function encodeUrl($url)
|
|
||||||
{
|
|
||||||
// Parse URL into pieces
|
|
||||||
$url_parsed = parse_url($url);
|
|
||||||
|
|
||||||
// Build the basics bypassing notices
|
$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 );
|
|
||||||
|
|
||||||
// Do we need to encode anything?
|
if ($query != null) {
|
||||||
if ($query != null) {
|
parse_str($url_parsed['query'], $query_parsed);
|
||||||
// Break up the query into an array
|
$query = '?' . http_build_query($query_parsed);
|
||||||
parse_str($url_parsed['query'], $query_parsed);
|
}
|
||||||
// Encode and build query based on RFC 1738
|
|
||||||
$query = '?'.http_build_query($query_parsed);
|
if ($port && $port[0] != ":")
|
||||||
}
|
$port = ":" . $port;
|
||||||
|
|
||||||
// Handle port seperator
|
|
||||||
if ($port && $port[0] != ":")
|
|
||||||
$port = ":" . $port;
|
|
||||||
|
|
||||||
// Return the completed URL
|
$result = $scheme . $host . $port . $path . $query;
|
||||||
$result = $scheme . $host . $port . $path . $query;
|
return $result;
|
||||||
return $result;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!function_exists('http_chunked_decode')) {
|
private static function getHeader($key, $val) {
|
||||||
/**
|
$key = trim(strtolower($key));
|
||||||
* dechunk an http 'transfer-encoding: chunked' message
|
if ($key == "user-agent" || $key == "expect") continue;
|
||||||
*
|
return $key . ": " . $val;
|
||||||
* @param string $chunk the encoded message
|
}
|
||||||
* @return string the decoded message. If $chunk wasn't encoded properly it will be returned unmodified.
|
|
||||||
*/
|
|
||||||
function http_chunked_decode($chunk) {
|
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if (!function_exists('http_chunked_decode')) {
|
||||||
* determine if a string can represent a number in hexadecimal
|
/**
|
||||||
*
|
* Dechunk an http 'transfer-encoding: chunked' message
|
||||||
* @param string $hex
|
* @param string $chunk the encoded message
|
||||||
* @return boolean true if the string is a hex, otherwise false
|
* @return string the decoded message
|
||||||
*/
|
*/
|
||||||
function is_hex($hex) {
|
function http_chunked_decode($chunk)
|
||||||
// regex is for weenies
|
{
|
||||||
$hex = strtolower(trim(ltrim($hex,"0")));
|
$pos = 0;
|
||||||
if (empty($hex)) { $hex = 0; };
|
$len = strlen($chunk);
|
||||||
$dec = hexdec($hex);
|
$dechunk = null;
|
||||||
return ($hex == dechex($dec));
|
|
||||||
}
|
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
|
||||||
|
* @link http://uk1.php.net/ctype_xdigit
|
||||||
|
* @param string $hex
|
||||||
|
* @return boolean true if the string is a hex, otherwise false
|
||||||
|
*/
|
||||||
|
function is_hex($hex)
|
||||||
|
{
|
||||||
|
return ctype_xdigit($hex);
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user