|
1
|
|
|
<?php |
|
2
|
|
|
namespace vipnytt\RobotsTxtParser; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp; |
|
5
|
|
|
use vipnytt\RobotsTxtParser\Parser\RobotsTxtInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Download |
|
9
|
|
|
* |
|
10
|
|
|
* @package vipnytt\RobotsTxtParser |
|
11
|
|
|
*/ |
|
12
|
|
|
class Download implements RobotsTxtInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Base uri |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $baseUri; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* GuzzleHttp response |
|
22
|
|
|
* @var \Psr\Http\Message\ResponseInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $response; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Download constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $baseUri |
|
30
|
|
|
* @param array $guzzleConfig |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($baseUri, $guzzleConfig = []) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->baseUri = $baseUri; |
|
35
|
|
|
$client = new GuzzleHttp\Client( |
|
36
|
|
|
array_merge_recursive( |
|
37
|
|
|
[ |
|
38
|
|
|
'allow_redirects' => [ |
|
39
|
|
|
'max' => self::MAX_REDIRECTS, |
|
40
|
|
|
], |
|
41
|
|
|
'base_uri' => $baseUri, |
|
42
|
|
|
'headers' => [ |
|
43
|
|
|
'Accept' => 'text/plain;q=1.0, text/*;q=0.8, */*;q=0.1', |
|
44
|
|
|
'Accept-Charset' => 'utf-8;q=1.0, *;q=0.1', |
|
45
|
|
|
'Accept-Encoding' => 'identity;q=1.0, *;q=0.1', |
|
46
|
|
|
'User-Agent' => 'RobotsTxtParser-VIPnytt/1.0 (+https://github.com/VIPnytt/RobotsTxtParser/blob/master/README.md)', |
|
47
|
|
|
], |
|
48
|
|
|
'http_errors' => false, |
|
49
|
|
|
'timeout' => 60, |
|
50
|
|
|
], |
|
51
|
|
|
$guzzleConfig |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
$this->response = $client->request('GET', '/robots.txt'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Parser client |
|
59
|
|
|
* |
|
60
|
|
|
* @param int|null $byteLimit |
|
61
|
|
|
* @return Client |
|
62
|
|
|
*/ |
|
63
|
|
|
public function parserClient($byteLimit = self::BYTE_LIMIT) |
|
64
|
|
|
{ |
|
65
|
|
|
return new Client($this->baseUri, $this->getStatusCode(), $this->getContents(), $this->getEncoding(), $byteLimit); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Status code |
|
70
|
|
|
* |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getStatusCode() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->response->getStatusCode(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* URL content |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getContents() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->response->getBody()->getContents(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Encoding |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getEncoding() |
|
94
|
|
|
{ |
|
95
|
|
|
$header = $this->response->getHeader('content-type')[0]; |
|
96
|
|
|
$split = array_map('trim', mb_split(';', $header)); |
|
97
|
|
|
foreach ($split as $string) { |
|
98
|
|
|
if (mb_stripos($string, 'charset=') === 0) { |
|
99
|
|
|
return mb_split('=', $string, 2)[1]; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
return $this->detectEncoding(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Manually detect encoding |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function detectEncoding() |
|
111
|
|
|
{ |
|
112
|
|
|
if (($encoding = mb_detect_encoding($this->getContents())) !== false) { |
|
113
|
|
|
return $encoding; |
|
114
|
|
|
} |
|
115
|
|
|
return self::ENCODING; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|