-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
621 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
language: php | ||
php: | ||
- '5.4' | ||
- '5.5' | ||
- '5.6' | ||
- '7.0' | ||
- '7.1' | ||
- hhvm | ||
- nightly | ||
|
||
before_script: | ||
- composer install | ||
|
||
script: | ||
- vendor/bin/phpcs | ||
- vendor/bin/phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0"?> | ||
<ruleset> | ||
<file>src</file> | ||
<file>test</file> | ||
<exclude-pattern>vendor/*</exclude-pattern> | ||
<rule ref="PSR2"/> | ||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
namespace PHPCurl\CurlHttp; | ||
|
||
use PHPCurl\CurlWrapper\Curl; | ||
use PHPCurl\CurlWrapper\CurlInterface; | ||
|
||
class Executor implements ExecutorInterface | ||
{ | ||
/** | ||
* @var CurlInterface | ||
*/ | ||
private $curl; | ||
|
||
/** | ||
* @param CurlInterface $curl | ||
*/ | ||
public function __construct(CurlInterface $curl = null) | ||
{ | ||
$this->curl = $curl ?: new Curl(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute($url, array $options) | ||
{ | ||
$options[CURLOPT_RETURNTRANSFER] = true; | ||
$options[CURLOPT_HEADER] = true; | ||
|
||
$this->curl->init($url); | ||
$this->curl->setOptArray($options); | ||
$response = $this->curl->exec(); | ||
if (false === $response) { | ||
throw NoResponse::fromCurl($this->curl); | ||
} | ||
$info = $this->curl->getInfo(); | ||
return HttpResponse::fromCurl($response, $info); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
namespace PHPCurl\CurlHttp; | ||
|
||
interface ExecutorInterface | ||
{ | ||
/** | ||
* Init curl with $url, set $options, execute, return response | ||
* @param string $url Goes to curl_init() | ||
* @param array $options Goes to curl_setopt() | ||
* @return HttpResponse | ||
* @throws NoResponse If no response was received | ||
*/ | ||
public function execute($url, array $options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,155 +1,110 @@ | ||
<?php | ||
namespace PHPCurl\CurlHttp; | ||
|
||
use \PHPCurl\CurlWrapper\Curl; | ||
|
||
/** | ||
* Minimalistic HTTP client | ||
*/ | ||
class HttpClient | ||
class HttpClient implements HttpClientInterface | ||
{ | ||
const USE_EXCEPTIONS = true; | ||
|
||
/** | ||
* Numer of attempts to make per each call | ||
* @var int | ||
* @var ExecutorInterface | ||
*/ | ||
protected $attempts = 1; | ||
private $executor; | ||
|
||
/** | ||
* @var array | ||
* HttpClient constructor. | ||
* @param ExecutorInterface $executor | ||
*/ | ||
private $userOptions = array(); | ||
public function __construct(ExecutorInterface $executor = null) | ||
{ | ||
$this->executor = $executor ?: new Executor(); | ||
} | ||
|
||
/** | ||
* HTTP GET | ||
* @param string $url Goes to curl_init() | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @param string $url Goes to curl_init() | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function get($url, array $headers = null) | ||
public function get($url, array $headers = []) | ||
{ | ||
$opt = array(); | ||
if ($headers) { | ||
$opt[CURLOPT_HTTPHEADER] = $headers; | ||
} | ||
return $this->exec($url, $opt); | ||
return $this->executor->execute( | ||
$url, | ||
[ | ||
CURLOPT_HTTPHEADER => $headers, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* HTTP HEAD (implemented using CURLOPT_NOBODY) | ||
* @param string $url Goes to curl_init() | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @param string $url Goes to curl_init() | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function head($url, array $headers = null) | ||
public function head($url, array $headers = []) | ||
{ | ||
$opt[CURLOPT_NOBODY] = true; | ||
if ($headers !== null) { | ||
$opt[CURLOPT_HTTPHEADER] = $headers; | ||
} | ||
return $this->exec($url, $opt); | ||
return $this->executor->execute( | ||
$url, | ||
[ | ||
CURLOPT_NOBODY => true, | ||
CURLOPT_HTTPHEADER => $headers, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* HTTP POST | ||
* @param string $url Goes to curl_init() | ||
* @param string|array $data Same as CURLOPT_POSTFIELDS | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @param string $url Goes to curl_init() | ||
* @param string|array $data Same as CURLOPT_POSTFIELDS | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function post($url, $data = null, array $headers = null) | ||
public function post($url, $data = '', array $headers = []) | ||
{ | ||
$opt[CURLOPT_POST] = true; | ||
if ($data !== null) { | ||
$opt[CURLOPT_POSTFIELDS] = $data; | ||
} | ||
if ($headers !== null) { | ||
$opt[CURLOPT_HTTPHEADER] = $headers; | ||
} | ||
return $this->exec($url, $opt); | ||
return $this->executor->execute( | ||
$url, | ||
[ | ||
CURLOPT_POST => true, | ||
CURLOPT_HTTPHEADER => $headers, | ||
CURLOPT_POSTFIELDS => $data, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* HTTP PUT | ||
* @param string $url Goes to curl_init() | ||
* @param string|array $data Same as CURLOPT_POSTFIELDS | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @param string $url Goes to curl_init() | ||
* @param string|array $data Same as CURLOPT_POSTFIELDS | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function put($url, $data = null, array $headers = null) | ||
public function put($url, $data = '', array $headers = []) | ||
{ | ||
return $this->request('PUT', $url, $data, $headers); | ||
return $this->executor->execute( | ||
$url, | ||
[ | ||
CURLOPT_CUSTOMREQUEST => 'PUT', | ||
CURLOPT_HTTPHEADER => $headers, | ||
CURLOPT_POSTFIELDS => $data, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* HTTP DELETE | ||
* @param string $url Goes to curl_init() | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function delete($url, array $headers = null) | ||
{ | ||
return $this->request('DELETE', $url, null, $headers); | ||
} | ||
|
||
/** | ||
* Custom HTTP method | ||
* @param string $method Goes to CURLOPT_CUSTOMREQUEST | ||
* @param string $url Goes to curl_init() | ||
* @param string|array $data Goes to CURLOPT_POSTFIELDS | ||
* @param array $headers Goes to CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function request($method, $url, $data = null, array $headers = null) | ||
{ | ||
$opt[CURLOPT_CUSTOMREQUEST] = $method; | ||
if ($headers !== null) { | ||
$opt[CURLOPT_HTTPHEADER] = $headers; | ||
} | ||
if ($data !== null) { | ||
$opt[CURLOPT_POSTFIELDS] = $data; | ||
} | ||
return $this->exec($url, $opt); | ||
} | ||
|
||
/** | ||
* Set additional CURL options to pass with each request | ||
* @param array $userOptions Format is the same as curl_setopt_array(). | ||
* Pass an empty array to clear. | ||
*/ | ||
public function setOptions(array $userOptions) | ||
{ | ||
$this->userOptions = $userOptions; | ||
} | ||
|
||
/** | ||
* Init curl with $url, set $options, execute, return response | ||
* @param string $url Goes to curl_init() | ||
* @param array $options Goes to curl_setopt() | ||
* @param Curl $curl | ||
* @param string $url Goes to curl_init() | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function exec($url, array $options, Curl $curl = null) | ||
public function delete($url, array $headers = []) | ||
{ | ||
$options[CURLOPT_RETURNTRANSFER] = true; | ||
$options[CURLOPT_HEADER] = true; | ||
|
||
$curl = $curl ?: new Curl(); | ||
$curl->init($url); | ||
$curl->setOptArray(array_replace_recursive( | ||
$this->userOptions, | ||
$options | ||
)); | ||
|
||
$response = $curl->exec($this->attempts, self::USE_EXCEPTIONS); | ||
|
||
$info = $curl->getInfo(); | ||
$code = $info['http_code']; | ||
$headerSize = $info['header_size']; | ||
$headers = substr($response, 0, $headerSize); | ||
$headersArray = preg_split("/\r\n/", $headers, -1, PREG_SPLIT_NO_EMPTY); | ||
$body = substr($response, $headerSize); | ||
return new HttpResponse($code, $headersArray, $body); | ||
return $this->executor->execute( | ||
$url, | ||
[ | ||
CURLOPT_CUSTOMREQUEST => 'DELETE', | ||
CURLOPT_HTTPHEADER => $headers, | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
namespace PHPCurl\CurlHttp; | ||
|
||
interface HttpClientInterface | ||
{ | ||
/** | ||
* HTTP GET | ||
* @param string $url Goes to curl_init() | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function get($url, array $headers = []); | ||
|
||
/** | ||
* HTTP HEAD (implemented using CURLOPT_NOBODY) | ||
* @param string $url Goes to curl_init() | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function head($url, array $headers = []); | ||
|
||
/** | ||
* HTTP POST | ||
* @param string $url Goes to curl_init() | ||
* @param string|array $data Same as CURLOPT_POSTFIELDS | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function post($url, $data = '', array $headers = []); | ||
|
||
/** | ||
* HTTP PUT | ||
* @param string $url Goes to curl_init() | ||
* @param string|array $data Same as CURLOPT_POSTFIELDS | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function put($url, $data = '', array $headers = []); | ||
|
||
/** | ||
* HTTP DELETE | ||
* @param string $url Goes to curl_init() | ||
* @param array $headers Same as CURLOPT_HEADER | ||
* @return HttpResponse | ||
*/ | ||
public function delete($url, array $headers = []); | ||
} |
Oops, something went wrong.