1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\NoCaptcha\Utilities; |
6
|
|
|
|
7
|
|
|
use Arcanedev\NoCaptcha\Contracts\Utilities\Request as RequestContract; |
8
|
|
|
use Arcanedev\NoCaptcha\Exceptions\InvalidUrlException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Request |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Request implements RequestContract |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* URL to request. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $url; |
28
|
|
|
|
29
|
|
|
/* ----------------------------------------------------------------- |
30
|
|
|
| Getters & Setters |
31
|
|
|
| ----------------------------------------------------------------- |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set URL. |
36
|
|
|
* |
37
|
|
|
* @param string $url |
38
|
|
|
* |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
20 |
|
protected function setUrl($url) |
42
|
|
|
{ |
43
|
20 |
|
$this->checkUrl($url); |
44
|
|
|
|
45
|
8 |
|
$this->url = $url; |
46
|
|
|
|
47
|
8 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* ----------------------------------------------------------------- |
51
|
|
|
| Main Methods |
52
|
|
|
| ----------------------------------------------------------------- |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create an api request using curl. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
8 |
|
protected function curl() |
61
|
|
|
{ |
62
|
8 |
|
$curl = curl_init(); |
63
|
8 |
|
curl_setopt_array($curl, [ |
64
|
8 |
|
CURLOPT_URL => $this->url, |
65
|
8 |
|
CURLOPT_RETURNTRANSFER => true, |
66
|
8 |
|
CURLOPT_SSL_VERIFYPEER => false, |
67
|
|
|
]); |
68
|
8 |
|
$result = curl_exec($curl); |
69
|
8 |
|
curl_close($curl); |
70
|
|
|
|
71
|
8 |
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Run the request and get response. |
76
|
|
|
* |
77
|
|
|
* @param string $url |
78
|
|
|
* @param bool $curled |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
20 |
|
public function send($url, $curled = true) |
83
|
|
|
{ |
84
|
20 |
|
$this->setUrl($url); |
85
|
|
|
|
86
|
8 |
|
$result = ($this->isCurlExists() && $curled === true) |
87
|
8 |
|
? $this->curl() |
88
|
8 |
|
: file_get_contents($this->url); |
89
|
|
|
|
90
|
8 |
|
return $this->checkResult($result) ? $result : '{}'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* ----------------------------------------------------------------- |
94
|
|
|
| Check Methods |
95
|
|
|
| ----------------------------------------------------------------- |
96
|
|
|
*/ |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check URL. |
100
|
|
|
* |
101
|
|
|
* @param string $url |
102
|
|
|
* |
103
|
|
|
* @throws \Arcanedev\NoCaptcha\Exceptions\InvalidUrlException |
104
|
|
|
*/ |
105
|
20 |
|
private function checkUrl(&$url): void |
106
|
|
|
{ |
107
|
20 |
|
if ( ! is_string($url)) |
108
|
4 |
|
throw new InvalidUrlException( |
109
|
4 |
|
'The url must be a string value, ' . gettype($url) . ' given' |
110
|
|
|
); |
111
|
|
|
|
112
|
16 |
|
$url = trim($url); |
113
|
|
|
|
114
|
16 |
|
if (empty($url)) |
115
|
4 |
|
throw new InvalidUrlException('The url must not be empty'); |
116
|
|
|
|
117
|
12 |
|
if (filter_var($url, FILTER_VALIDATE_URL) === false) |
118
|
4 |
|
throw new InvalidUrlException('The url [' . $url . '] is invalid'); |
119
|
8 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Check if curl exists. |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
8 |
|
private function isCurlExists(): bool |
127
|
|
|
{ |
128
|
8 |
|
return function_exists('curl_version'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Check Result. |
133
|
|
|
* |
134
|
|
|
* @param string $result |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
8 |
|
private function checkResult($result): bool |
139
|
|
|
{ |
140
|
8 |
|
return is_string($result) && ! empty($result); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|