1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2019 - 2024 Daniel Popiniuc |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace danielgp\io_operations; |
28
|
|
|
|
29
|
|
|
trait InputOutputCurl |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
public function getContentFromUrlThroughCurl(string $fullURL, array $features = []) |
33
|
|
|
{ |
34
|
|
|
$chanel = curl_init(); |
35
|
|
|
$inScopeUrl = $this->validateUrl($fullURL); |
36
|
|
|
$this->handleCurlOptions($chanel, $inScopeUrl, $features); |
|
|
|
|
37
|
|
|
$aReturn = [ |
38
|
|
|
'response' => curl_exec($chanel), |
39
|
|
|
'errNo' => curl_errno($chanel), |
40
|
|
|
'errMsg' => curl_error($chanel), |
41
|
|
|
]; |
42
|
|
|
if (array_key_exists('includeStatistics', $features)) { |
43
|
|
|
$aReturn['info'] = curl_getinfo($chanel); |
44
|
|
|
} |
45
|
|
|
if (array_key_exists('NoBody', $features)) { |
46
|
|
|
unset($aReturn['response']); |
47
|
|
|
} |
48
|
|
|
curl_close($chanel); |
49
|
|
|
return $aReturn; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function handleCurlOptions(\CurlHandle|false $chanel, string $inScopeUrl, array $features): void |
53
|
|
|
{ |
54
|
|
|
$this->setUserAgent($chanel, $features); |
55
|
|
|
$this->handleSecureConnection($chanel, $inScopeUrl, $features); |
56
|
|
|
$this->setForceIpv4($chanel, $features); |
57
|
|
|
$this->setNoBody($chanel, $features); |
58
|
|
|
curl_setopt($chanel, CURLOPT_FAILONERROR, true); |
|
|
|
|
59
|
|
|
curl_setopt($chanel, CURLOPT_FOLLOWLOCATION, true); |
60
|
|
|
curl_setopt($chanel, CURLOPT_FRESH_CONNECT, true); //avoid a cached response |
61
|
|
|
curl_setopt($chanel, CURLOPT_HEADER, false); |
62
|
|
|
curl_setopt($chanel, CURLOPT_MAXREDIRS, 10); |
63
|
|
|
curl_setopt($chanel, CURLOPT_RETURNTRANSFER, true); |
64
|
|
|
curl_setopt($chanel, CURLOPT_TCP_FASTOPEN, true); |
65
|
|
|
curl_setopt($chanel, CURLOPT_URL, $inScopeUrl); |
66
|
|
|
$this->setPostingDetails($chanel, $features); |
67
|
|
|
if (array_key_exists('HttpHeader', $features) && !array_key_exists('PostFields', $features)) { |
68
|
|
|
curl_setopt($chanel, CURLOPT_HTTPHEADER, $features['HttpHeader']); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function handleSecureConnection(\CurlHandle|false $chanel, string $fullURL, array $features): void |
73
|
|
|
{ |
74
|
|
|
if ((substr(strtolower($fullURL), 0, 5) === 'https')) { |
75
|
|
|
$chk = false; |
76
|
|
|
if (array_key_exists('forceSSLverification', $features)) { |
77
|
|
|
$chk = true; |
78
|
|
|
} |
79
|
|
|
curl_setopt($chanel, CURLOPT_SSL_VERIFYHOST, $chk); |
|
|
|
|
80
|
|
|
curl_setopt($chanel, CURLOPT_SSL_VERIFYPEER, $chk); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function setForceIpv4(\CurlHandle|false $chanel, array $features): void |
85
|
|
|
{ |
86
|
|
|
if (array_key_exists('forceIpV4', $features)) { |
87
|
|
|
if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) { |
88
|
|
|
curl_setopt($chanel, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function setNoBody(\CurlHandle|false $chanel, array $features): void |
94
|
|
|
{ |
95
|
|
|
if (array_key_exists('NoBody', $features)) { |
96
|
|
|
curl_setopt($chanel, CURLOPT_NOBODY, true); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function setPostingDetails(\CurlHandle|false $chanel, array $features): void |
101
|
|
|
{ |
102
|
|
|
if (array_key_exists('PostFields', $features)) { |
103
|
|
|
curl_setopt($chanel, CURLOPT_POST, true); |
|
|
|
|
104
|
|
|
curl_setopt($chanel, CURLOPT_POSTFIELDS, $features['PostFields']); |
105
|
|
|
curl_setopt($chanel, CURLOPT_HTTPHEADER, $features['HttpHeader']); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function setUserAgent(\CurlHandle|false $chanel, array $features): void |
110
|
|
|
{ |
111
|
|
|
if (array_key_exists('setUserAgent', $features)) { |
112
|
|
|
curl_setopt($chanel, CURLOPT_USERAGENT, $features['setUserAgent']); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function validateUrl(string $strUrl): string |
117
|
|
|
{ |
118
|
|
|
$inScopeUrl = filter_var($strUrl, FILTER_VALIDATE_URL); |
119
|
|
|
if ($inScopeUrl === false) { |
120
|
|
|
throw new \Exception('Invalid URL provided...'); |
121
|
|
|
} |
122
|
|
|
return $inScopeUrl; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|