76 public static function getIP(
bool $force =
false) : string|false{
79 }elseif(self::$ip !==
false && !$force){
85 return self::$ip = $ip->getBody();
89 if($ip !==
null && preg_match(
'#Current IP Address\: ([0-9a-fA-F\:\.]*)#', trim(strip_tags($ip->getBody())), $matches) > 0){
90 return self::$ip = $matches[1];
94 if($ip !==
null && preg_match(
'#">([0-9a-fA-F\:\.]*)</span>#', $ip->getBody(), $matches) > 0){
95 return self::$ip = $matches[1];
99 if($ip !==
null && preg_match(
'#Your IP address is ([0-9a-fA-F\:\.]*)#', $ip->getBody(), $matches) > 0){
100 return self::$ip = $matches[1];
104 if($ip !==
null && ($addr = trim($ip->getBody())) !==
""){
105 return self::$ip = $addr;
118 $sock = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
120 throw new InternetException(
"Failed to get internal IP: " . trim(socket_strerror(socket_last_error())));
123 if(!@socket_connect($sock,
"8.8.8.8", 65534)){
124 throw new InternetException(
"Failed to get internal IP: " . trim(socket_strerror(socket_last_error($sock))));
126 if(!@socket_getsockname($sock, $name)){
127 throw new InternetException(
"Failed to get internal IP: " . trim(socket_strerror(socket_last_error($sock))));
173 return self::simpleCurl($page, $timeout, $extraHeaders, [
175 CURLOPT_POSTFIELDS => $args
178 $err = $ex->getMessage();
197 public static function simpleCurl(
string $page,
float $timeout = 10, array $extraHeaders = [], array $extraOpts = [], ?\Closure $onSuccess =
null) :
InternetRequestResult{
202 $ch = curl_init($page);
207 curl_setopt_array($ch, $extraOpts + [
208 CURLOPT_SSL_VERIFYPEER =>
false,
209 CURLOPT_SSL_VERIFYHOST => 2,
210 CURLOPT_FORBID_REUSE => 1,
211 CURLOPT_FRESH_CONNECT => 1,
212 CURLOPT_AUTOREFERER =>
true,
213 CURLOPT_FOLLOWLOCATION =>
true,
214 CURLOPT_RETURNTRANSFER =>
true,
215 CURLOPT_CONNECTTIMEOUT_MS => (
int) ($timeout * 1000),
216 CURLOPT_TIMEOUT_MS => (
int) ($timeout * 1000),
217 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),
218 CURLOPT_HEADER =>
true
221 $raw = curl_exec($ch);
223 throw new InternetException(curl_error($ch));
225 if(!is_string($raw))
throw new AssumptionFailedError(
"curl_exec() should return string|false when CURLOPT_RETURNTRANSFER is set");
226 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
227 $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
228 $rawHeaders = substr($raw, 0, $headerSize);
229 $body = substr($raw, $headerSize);
232 foreach(explode(
"\r\n\r\n", $rawHeaders, limit: PHP_INT_MAX) as $rawHeaderGroup){
234 foreach(explode(
"\r\n", $rawHeaderGroup, limit: PHP_INT_MAX) as $line){
235 $nameValue = explode(
":", $line, 2);
236 if(isset($nameValue[1])){
237 $headerGroup[trim(strtolower($nameValue[0]))] = trim($nameValue[1]);
240 $headers[] = $headerGroup;
242 if($onSuccess !==
null){
245 return new InternetRequestResult($headers, $body, $httpCode);