|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Viirre\UrlChecker; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
7
|
|
|
use GuzzleHttp\Exception\ConnectException; |
|
8
|
|
|
|
|
9
|
|
|
class Checker |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Client |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $guzzle; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Create a new Instance |
|
18
|
|
|
*/ |
|
19
|
5 |
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
5 |
|
$this->guzzle = new Client(); |
|
22
|
5 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Perform the check of a URL |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $url URL to check |
|
28
|
|
|
* @param int $timeout timeout for the request. Defaults to 5 seconds |
|
29
|
|
|
* @return UrlStatus |
|
30
|
|
|
* @throws UrlMalformedException |
|
31
|
|
|
*/ |
|
32
|
5 |
|
public function check($url, $timeout = 5) |
|
33
|
|
|
{ |
|
34
|
5 |
|
$this->validateUrl($url); |
|
35
|
|
|
|
|
36
|
4 |
|
$response = null; |
|
37
|
4 |
|
$statusCode = null; |
|
38
|
4 |
|
$reason = null; |
|
39
|
4 |
|
$unresolved = false; |
|
40
|
4 |
|
$timeStart = microtime(true); |
|
41
|
|
|
|
|
42
|
|
|
try { |
|
43
|
|
|
|
|
44
|
4 |
|
$response = $this->guzzle->get($url, [ |
|
45
|
4 |
|
'timeout' => $timeout |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
2 |
|
$statusCode = $response->getStatusCode(); |
|
49
|
|
|
|
|
50
|
2 |
|
} catch (ClientException $e) { |
|
51
|
|
|
|
|
52
|
|
|
// When not a 200 status but still responding |
|
53
|
1 |
|
$statusCode = $e->getCode(); |
|
54
|
1 |
|
$reason = $e->getMessage(); |
|
55
|
|
|
|
|
56
|
1 |
|
} catch (ConnectException $e) { |
|
57
|
|
|
|
|
58
|
|
|
// Unresolvable host etc |
|
59
|
1 |
|
$reason = $e->getMessage(); |
|
60
|
1 |
|
$unresolved = true; |
|
61
|
|
|
|
|
62
|
|
|
} catch (\Exception $e) { |
|
63
|
|
|
|
|
64
|
|
|
// Other errors |
|
65
|
|
|
$reason = $e->getMessage(); |
|
66
|
|
|
$unresolved = true; |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
4 |
|
$timeEnd = microtime(true); |
|
71
|
4 |
|
$time = ($timeEnd - $timeStart); // seconds |
|
72
|
|
|
|
|
73
|
4 |
|
return new UrlStatus($url, $statusCode, $time, $unresolved, $response, $reason); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Validate that a url is a valid url |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $url |
|
80
|
|
|
* @throws UrlMalformedException |
|
81
|
|
|
*/ |
|
82
|
5 |
|
private function validateUrl($url) |
|
83
|
|
|
{ |
|
84
|
5 |
|
if (!filter_var($url, FILTER_VALIDATE_URL)) { |
|
85
|
1 |
|
throw new UrlMalformedException("The provided url: $url is malformed"); |
|
86
|
|
|
} |
|
87
|
4 |
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: