Skip to content

Commit

Permalink
Improved HTTP client / request options
Browse files Browse the repository at this point in the history
  • Loading branch information
jkphl committed May 29, 2017
1 parent ff79ced commit d3379f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,13 @@ All Notable changes to *jkphl/dom-factory* will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.


## [0.1.1] - 2017-05-29

### Added

* Improved HTTP client / request options

## [0.1.0] - 2017-05-24

Initial release
15 changes: 11 additions & 4 deletions doc/index.md
Expand Up @@ -30,13 +30,20 @@ $dom = Dom::createFromFile('/path/to/file.html');

```php
use Jkphl\Domfactory\Ports\Dom;
$dom = Dom::createFromUri('https://example.com/path/to/file.xml', ['timeout' => 30]);
$dom = Dom::createFromUri('https://example.com/path/to/file.xml');

// With HTTP client options
$options = [
'client' => ['timeout' => 30],
'request' => ['verify' => false],
];
$dom = Dom::createFromUri('https://example.com/path/to/file.xml', $options);
```

If the [cURL](http://php.net/manual/book.curl.php) PHP extension is available on your system the factory uses [Guzzle](http://docs.guzzlephp.org) to fetch the web resource or fall back to a native `file_get_contents()` otherwise. Depending on the request mechanism, the second (array) parameter for `Dom::createFromUri()` will be used
If the [cURL](http://php.net/manual/book.curl.php) PHP extension is available on your system the factory uses [Guzzle](http://docs.guzzlephp.org) to fetch the web resource or otherwise fall back to a native `file_get_contents()` strategy. Depending on the request mechanism, the second (array) parameter for `Dom::createFromUri()` will be used

* as [Guzzle request options](http://docs.guzzlephp.org/en/latest/request-options.html) or
* as `$options` for creating a PHP [stream context](http://php.net/manual/function.stream-context-create.php).
* as Guzzle [client](http://docs.guzzlephp.org/en/latest/quickstart.html#creating-a-client) / [request options](http://docs.guzzlephp.org/en/latest/request-options.html) or
* as `$options` for creating a PHP [stream context](http://php.net/manual/function.stream-context-create.php) (`client` key) respectively [setting its parameters](http://php.net/manual/en/function.stream-context-set-params.php) (`request` key).

Please make sure to pass in a compatible set of options.

Expand Down
11 changes: 8 additions & 3 deletions src/Domfactory/Infrastructure/Dom.php
Expand Up @@ -63,8 +63,10 @@ protected static function createViaHttpClient($url, array $options = [])
{
try {
$guzzleUrl = Url::factory($url);
$client = new Client($guzzleUrl, array_merge(['timeout' => 10.0], $options));
$request = $client->get($guzzleUrl);
$clientOptions = isset($options['client']) ? (array)$options['client'] : [];
$client = new Client($guzzleUrl, array_merge(['timeout' => 10.0], $clientOptions));
$requestOptions = isset($options['request']) ? (array)$options['request'] : [];
$request = $client->get($guzzleUrl, null, $requestOptions);
$response = $client->send($request);
return self::createFromString(strval($response->getBody()));

Expand Down Expand Up @@ -95,6 +97,7 @@ public static function createFromString($str)
*/
protected static function createViaStreamWrapper($url, array $options = [])
{
$clientOptions = isset($options['client']) ? (array)$options['client'] : [];
$opts = array_merge_recursive([
'http' => [
'method' => 'GET',
Expand All @@ -104,8 +107,10 @@ protected static function createViaStreamWrapper($url, array $options = [])
'timeout' => 10.0,
'header' => "Accept-language: en\r\n",
]
], $options);
], $clientOptions);
$context = stream_context_create($opts);
$requestOptions = isset($options['request']) ? (array)$options['request'] : [];
stream_context_set_params($context, $requestOptions);
$response = @file_get_contents($url, false, $context);
return self::createFromString($response);
}
Expand Down

0 comments on commit d3379f9

Please sign in to comment.