Skip to content

v2 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 21, 2017
Merged

v2 #2

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .travis.yml
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
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,14 @@ Via [composer](https://getcomposer.org):
`$ composer require "phpcurl/curlhttp"`

##Usage

It is really that easy.

```php
<?php
require_once 'vendor/autoload.php';
use PHPCurl\CurlHttp\HttpClient;

$http = new HttpClient();

$http->setOptions([
CURLOPT_FOLLOWLOCATION => false, // Any arbitrary curl options you want
]);

$response = $http->post('http://example.com/?a=b', 'my post data', ['User-Agent: My php crawler']);
// Supported: get(), post(), head(), post(), put(), delete(), custom methods
// Supported: get(), post(), head(), post(), put(), delete()

$body = $response->getBody(); // Response body, string

Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
}
],
"require": {
"php": ">=5.3.0",
"phpcurl/curlwrapper": "^1"
"php": ">=5.5",
"phpcurl/curlwrapper": "^2.1"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"weew/php-http-server": "^1.0.0"
"phpunit/phpunit": "^4.0 || ^5.0",
"squizlabs/php_codesniffer": "^2.0",
"symfony/process": "^2"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions phpcs.xml.dist
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>
2 changes: 0 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
bootstrap="vendor/autoload.php"
backupGlobals="true"
backupStaticAttributes="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
39 changes: 39 additions & 0 deletions src/Executor.php
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);
}
}
14 changes: 14 additions & 0 deletions src/ExecutorInterface.php
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);
}
169 changes: 62 additions & 107 deletions src/HttpClient.php
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,
]
);
}
}
47 changes: 47 additions & 0 deletions src/HttpClientInterface.php
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 = []);
}
Loading