22declare(strict_types=1);
27use
function array_merge;
28use
function curl_close;
29use
function curl_error;
30use
function curl_exec;
31use
function curl_getinfo;
32use
function curl_init;
33use
function curl_setopt_array;
35use
function is_string;
36use
function preg_match;
37use
function socket_close;
38use
function socket_connect;
39use
function socket_create;
40use
function socket_getsockname;
41use
function socket_last_error;
42use
function socket_strerror;
43use
function strip_tags;
44use
function strtolower;
48use
const CURLINFO_HEADER_SIZE;
49use
const CURLINFO_HTTP_CODE;
50use
const CURLOPT_AUTOREFERER;
51use
const CURLOPT_CONNECTTIMEOUT_MS;
52use
const CURLOPT_FOLLOWLOCATION;
53use
const CURLOPT_FORBID_REUSE;
54use
const CURLOPT_FRESH_CONNECT;
55use
const CURLOPT_HEADER;
56use
const CURLOPT_HTTPHEADER;
57use
const CURLOPT_POST;
58use
const CURLOPT_POSTFIELDS;
59use
const CURLOPT_RETURNTRANSFER;
60use
const CURLOPT_SSL_VERIFYHOST;
61use
const CURLOPT_SSL_VERIFYPEER;
62use
const CURLOPT_TIMEOUT_MS;
67 public static string|
false $ip =
false;
68 public static bool $online =
true;
75 public static function getIP(
bool $force =
false) : string|false{
78 }elseif(self::$ip !==
false && !$force){
84 return self::$ip = $ip->getBody();
88 if($ip !==
null && preg_match(
'#Current IP Address\: ([0-9a-fA-F\:\.]*)#', trim(strip_tags($ip->getBody())), $matches) > 0){
89 return self::$ip = $matches[1];
93 if($ip !==
null && preg_match(
'#">([0-9a-fA-F\:\.]*)</span>#', $ip->getBody(), $matches) > 0){
94 return self::$ip = $matches[1];
98 if($ip !==
null && preg_match(
'#Your IP address is ([0-9a-fA-F\:\.]*)#', $ip->getBody(), $matches) > 0){
99 return self::$ip = $matches[1];
103 if($ip !==
null && ($addr = trim($ip->getBody())) !=
""){
104 return self::$ip = $addr;
117 $sock = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
119 throw new InternetException(
"Failed to get internal IP: " . trim(socket_strerror(socket_last_error())));
122 if(!@socket_connect($sock,
"8.8.8.8", 65534)){
123 throw new InternetException(
"Failed to get internal IP: " . trim(socket_strerror(socket_last_error($sock))));
125 if(!@socket_getsockname($sock, $name)){
126 throw new InternetException(
"Failed to get internal IP: " . trim(socket_strerror(socket_last_error($sock))));
149 return self::simpleCurl($page, $timeout, $extraHeaders);
151 $err = $ex->getMessage();
172 return self::simpleCurl($page, $timeout, $extraHeaders, [
174 CURLOPT_POSTFIELDS => $args
177 $err = $ex->getMessage();
196 public static function simpleCurl(
string $page,
float $timeout = 10, array $extraHeaders = [], array $extraOpts = [], ?\Closure $onSuccess =
null) :
InternetRequestResult{
201 $ch = curl_init($page);
206 curl_setopt_array($ch, $extraOpts + [
207 CURLOPT_SSL_VERIFYPEER =>
false,
208 CURLOPT_SSL_VERIFYHOST => 2,
209 CURLOPT_FORBID_REUSE => 1,
210 CURLOPT_FRESH_CONNECT => 1,
211 CURLOPT_AUTOREFERER =>
true,
212 CURLOPT_FOLLOWLOCATION =>
true,
213 CURLOPT_RETURNTRANSFER =>
true,
214 CURLOPT_CONNECTTIMEOUT_MS => (
int) ($timeout * 1000),
215 CURLOPT_TIMEOUT_MS => (
int) ($timeout * 1000),
216 CURLOPT_HTTPHEADER => array_merge([
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 " . VersionInfo::NAME .
"/" . VersionInfo::VERSION()->getFullVersion(
true)], $extraHeaders),
217 CURLOPT_HEADER =>
true
220 $raw = curl_exec($ch);
222 throw new InternetException(curl_error($ch));
224 if(!is_string($raw))
throw new AssumptionFailedError(
"curl_exec() should return string|false when CURLOPT_RETURNTRANSFER is set");
225 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
226 $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
227 $rawHeaders = substr($raw, 0, $headerSize);
228 $body = substr($raw, $headerSize);
230 foreach(explode(
"\r\n\r\n", $rawHeaders) as $rawHeaderGroup){
232 foreach(explode(
"\r\n", $rawHeaderGroup) as $line){
233 $nameValue = explode(
":", $line, 2);
234 if(isset($nameValue[1])){
235 $headerGroup[trim(strtolower($nameValue[0]))] = trim($nameValue[1]);
238 $headers[] = $headerGroup;
240 if($onSuccess !==
null){
243 return new InternetRequestResult($headers, $body, $httpCode);
static getIP(bool $force=false)
static simpleCurl(string $page, float $timeout=10, array $extraHeaders=[], array $extraOpts=[], ?\Closure $onSuccess=null)
static getURL(string $page, int $timeout=10, array $extraHeaders=[], &$err=null)
static postURL(string $page, array|string $args, int $timeout=10, array $extraHeaders=[], &$err=null)