1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* dom-factory |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Domfactory |
8
|
|
|
* @subpackage Jkphl\Domfactory\Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Domfactory\Infrastructure; |
38
|
|
|
|
39
|
|
|
use Guzzle\Common\Exception\RuntimeException as GuzzleRuntimeException; |
40
|
|
|
use Guzzle\Http\Client; |
41
|
|
|
use Guzzle\Http\Url; |
42
|
|
|
use Jkphl\Domfactory\Domain\Dom as DomainDom; |
43
|
|
|
use Jkphl\Domfactory\Ports\RuntimeException; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* DOM factory (internal) |
47
|
|
|
* |
48
|
|
|
* @package Jkphl\Domfactory |
49
|
|
|
* @subpackage Jkphl\Domfactory\Infrastructure |
50
|
|
|
*/ |
51
|
|
|
class Dom |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Create a DOM document using a HTTP client implementation |
55
|
|
|
* |
56
|
|
|
* @param string $url HTTP / HTTPS URL |
57
|
|
|
* @param array $options Connection options |
58
|
|
|
* @return \DOMDocument DOM document |
59
|
|
|
* @throws RuntimeException If the request wasn't successful |
60
|
|
|
* @throws RuntimeException If a runtime exception occurred |
61
|
|
|
*/ |
62
|
1 |
|
protected static function createViaHttpClient($url, array $options = []) |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
1 |
|
$guzzleUrl = Url::factory($url); |
66
|
1 |
|
$clientOptions = isset($options['client']) ? (array)$options['client'] : []; |
67
|
1 |
|
$client = new Client($guzzleUrl, array_merge(['timeout' => 10.0], $clientOptions)); |
68
|
1 |
|
$requestOptions = isset($options['request']) ? (array)$options['request'] : []; |
69
|
1 |
|
$request = $client->get($guzzleUrl, null, $requestOptions); |
70
|
1 |
|
$response = $client->send($request); |
71
|
|
|
return self::createFromString(strval($response->getBody())); |
72
|
|
|
|
73
|
|
|
// If a runtime exception occurred |
74
|
1 |
|
} catch (GuzzleRuntimeException $e) { |
75
|
1 |
|
throw new RuntimeException($e->getMessage(), $e->getCode()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Create a DOM document from a string |
81
|
|
|
* |
82
|
|
|
* @param string $str String |
83
|
|
|
* @return \DOMDocument DOM document |
84
|
|
|
*/ |
85
|
8 |
|
public static function createFromString($str) |
86
|
|
|
{ |
87
|
8 |
|
$dom = new DomainDom(new HtmlParser()); |
88
|
8 |
|
return $dom->load($str); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create a DOM document via the PHP stream wrapper |
93
|
|
|
* |
94
|
|
|
* @param string $url URL |
95
|
|
|
* @param array $options Connection options |
96
|
|
|
* @return \DOMDocument DOM document |
97
|
|
|
*/ |
98
|
2 |
|
protected static function createViaStreamWrapper($url, array $options = []) |
99
|
|
|
{ |
100
|
2 |
|
$clientOptions = isset($options['client']) ? (array)$options['client'] : []; |
101
|
2 |
|
$opts = array_merge_recursive([ |
102
|
|
|
'http' => [ |
103
|
2 |
|
'method' => 'GET', |
104
|
2 |
|
'protocol_version' => 1.1, |
105
|
2 |
|
'user_agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.466.4 Safari/534.3', |
106
|
2 |
|
'max_redirects' => 10, |
107
|
2 |
|
'timeout' => 10.0, |
108
|
2 |
|
'header' => "Accept-language: en\r\n", |
109
|
|
|
] |
110
|
2 |
|
], $clientOptions); |
111
|
2 |
|
$context = stream_context_create($opts); |
112
|
2 |
|
$requestOptions = isset($options['request']) ? (array)$options['request'] : []; |
113
|
2 |
|
stream_context_set_params($context, $requestOptions); |
114
|
2 |
|
$response = @file_get_contents($url, false, $context); |
115
|
2 |
|
return self::createFromString($response); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|