| @@ 339-417 (lines=79) @@ | ||
| 336 | * @return bool|false|string |
|
| 337 | * @return bool|false|string |
|
| 338 | */ |
|
| 339 | public function fetchURL($url, $methods = ['fopen', 'curl', 'socket']) |
|
| 340 | { |
|
| 341 | /** |
|
| 342 | * December 21st 2010, Mathew Tinsley ([email protected]) |
|
| 343 | * http://tinsology.net |
|
| 344 | * |
|
| 345 | * To the extent possible under law, Mathew Tinsley has waived all copyright and related or |
|
| 346 | * neighboring rights to this work. There's absolutely no warranty. |
|
| 347 | */ |
|
| 348 | if ('string' === gettype($methods)) { |
|
| 349 | $methods = [$methods]; |
|
| 350 | } elseif (!is_array($methods)) { |
|
| 351 | return false; |
|
| 352 | } |
|
| 353 | foreach ($methods as $method) { |
|
| 354 | switch ($method) { |
|
| 355 | case 'fopen': |
|
| 356 | //uses file_get_contents in place of fopen |
|
| 357 | //allow_url_fopen must still be enabled |
|
| 358 | if (ini_get('allow_url_fopen')) { |
|
| 359 | $contents = file_get_contents($url); |
|
| 360 | if (false !== $contents) { |
|
| 361 | return $contents; |
|
| 362 | } |
|
| 363 | } |
|
| 364 | break; |
|
| 365 | case 'curl': |
|
| 366 | if (function_exists('curl_init')) { |
|
| 367 | $ch = curl_init(); |
|
| 368 | curl_setopt($ch, CURLOPT_URL, $url); |
|
| 369 | curl_setopt($ch, CURLOPT_HEADER, 0); |
|
| 370 | // return the value instead of printing the response to browser |
|
| 371 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 372 | $result = curl_exec($ch); |
|
| 373 | curl_close($ch); |
|
| 374 | //return curl_exec($ch); |
|
| 375 | return $result; |
|
| 376 | } |
|
| 377 | break; |
|
| 378 | case 'socket': |
|
| 379 | //make sure the url contains a protocol, otherwise $parts['host'] won't be set |
|
| 380 | if (0 !== mb_strpos($url, 'http://') && 0 !== mb_strpos($url, 'https://')) { |
|
| 381 | $url = 'http://' . $url; |
|
| 382 | } |
|
| 383 | $parts = parse_url($url); |
|
| 384 | if ('https' === $parts['scheme']) { |
|
| 385 | $target = 'ssl://' . $parts['host']; |
|
| 386 | $port = isset($parts['port']) ? $parts['port'] : 443; |
|
| 387 | } else { |
|
| 388 | $target = $parts['host']; |
|
| 389 | $port = isset($parts['port']) ? $parts['port'] : 80; |
|
| 390 | } |
|
| 391 | $page = isset($parts['path']) ? $parts['path'] : ''; |
|
| 392 | $page .= isset($parts['query']) ? '?' . $parts['query'] : ''; |
|
| 393 | $page .= isset($parts['fragment']) ? '#' . $parts['fragment'] : ''; |
|
| 394 | $page = ('' == $page) ? '/' : $page; |
|
| 395 | $fp = fsockopen($target, $port, $errno, $errstr, 15); |
|
| 396 | if ($fp) { |
|
| 397 | $headers = "GET $page HTTP/1.1\r\n"; |
|
| 398 | $headers .= "Host: {$parts['host']}\r\n"; |
|
| 399 | $headers .= "Connection: Close\r\n\r\n"; |
|
| 400 | if (fwrite($fp, $headers)) { |
|
| 401 | $resp = ''; |
|
| 402 | //while not eof and an error does not occur when calling fgets |
|
| 403 | while (!feof($fp) && false !== ($curr = fgets($fp, 128))) { |
|
| 404 | $resp .= $curr; |
|
| 405 | } |
|
| 406 | if (isset($curr) && false !== $curr) { |
|
| 407 | return mb_substr(mb_strstr($resp, "\r\n\r\n"), 3); |
|
| 408 | } |
|
| 409 | } |
|
| 410 | fclose($fp); |
|
| 411 | } |
|
| 412 | break; |
|
| 413 | } |
|
| 414 | } |
|
| 415 | ||
| 416 | return false; |
|
| 417 | } |
|
| 418 | ||
| 419 | /** |
|
| 420 | * Smallworld_sanitize(array(array) ) |
|
| @@ 336-413 (lines=78) @@ | ||
| 333 | * @param array $methods |
|
| 334 | * @returns string |
|
| 335 | */ |
|
| 336 | public function fetchURL($url, $methods = ['fopen', 'curl', 'socket']) |
|
| 337 | { |
|
| 338 | /** |
|
| 339 | * December 21st 2010, Mathew Tinsley ([email protected]) |
|
| 340 | * http://tinsology.net |
|
| 341 | * |
|
| 342 | * To the extent possible under law, Mathew Tinsley has waived all copyright and related or |
|
| 343 | * neighboring rights to this work. There's absolutely no warranty. |
|
| 344 | */ |
|
| 345 | if ('string' === gettype($methods)) { |
|
| 346 | $methods = [$methods]; |
|
| 347 | } elseif (!is_array($methods)) { |
|
| 348 | return false; |
|
| 349 | } |
|
| 350 | foreach ($methods as $method) { |
|
| 351 | switch ($method) { |
|
| 352 | case 'fopen': |
|
| 353 | //uses file_get_contents in place of fopen |
|
| 354 | //allow_url_fopen must still be enabled |
|
| 355 | if (ini_get('allow_url_fopen')) { |
|
| 356 | $contents = file_get_contents($url); |
|
| 357 | if (false !== $contents) { |
|
| 358 | return $contents; |
|
| 359 | } |
|
| 360 | } |
|
| 361 | break; |
|
| 362 | case 'curl': |
|
| 363 | if (function_exists('curl_init')) { |
|
| 364 | $ch = curl_init(); |
|
| 365 | curl_setopt($ch, CURLOPT_URL, $url); |
|
| 366 | curl_setopt($ch, CURLOPT_HEADER, 0); |
|
| 367 | // return the value instead of printing the response to browser |
|
| 368 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 369 | $result = curl_exec($ch); |
|
| 370 | curl_close($ch); |
|
| 371 | //return curl_exec($ch); |
|
| 372 | return $result; |
|
| 373 | } |
|
| 374 | break; |
|
| 375 | case 'socket': |
|
| 376 | //make sure the url contains a protocol, otherwise $parts['host'] won't be set |
|
| 377 | if (0 !== strpos($url, 'http://') && 0 !== strpos($url, 'https://')) { |
|
| 378 | $url = 'http://' . $url; |
|
| 379 | } |
|
| 380 | $parts = parse_url($url); |
|
| 381 | if ('https' === $parts['scheme']) { |
|
| 382 | $target = 'ssl://' . $parts['host']; |
|
| 383 | $port = isset($parts['port']) ? $parts['port'] : 443; |
|
| 384 | } else { |
|
| 385 | $target = $parts['host']; |
|
| 386 | $port = isset($parts['port']) ? $parts['port'] : 80; |
|
| 387 | } |
|
| 388 | $page = isset($parts['path']) ? $parts['path'] : ''; |
|
| 389 | $page .= isset($parts['query']) ? '?' . $parts['query'] : ''; |
|
| 390 | $page .= isset($parts['fragment']) ? '#' . $parts['fragment'] : ''; |
|
| 391 | $page = ('' == $page) ? '/' : $page; |
|
| 392 | if ($fp = fsockopen($target, $port, $errno, $errstr, 15)) { |
|
| 393 | $headers = "GET $page HTTP/1.1\r\n"; |
|
| 394 | $headers .= "Host: {$parts['host']}\r\n"; |
|
| 395 | $headers .= "Connection: Close\r\n\r\n"; |
|
| 396 | if (fwrite($fp, $headers)) { |
|
| 397 | $resp = ''; |
|
| 398 | //while not eof and an error does not occur when calling fgets |
|
| 399 | while (!feof($fp) && false !== ($curr = fgets($fp, 128))) { |
|
| 400 | $resp .= $curr; |
|
| 401 | } |
|
| 402 | if (isset($curr) && false !== $curr) { |
|
| 403 | return substr(strstr($resp, "\r\n\r\n"), 3); |
|
| 404 | } |
|
| 405 | } |
|
| 406 | fclose($fp); |
|
| 407 | } |
|
| 408 | break; |
|
| 409 | } |
|
| 410 | } |
|
| 411 | ||
| 412 | return false; |
|
| 413 | } |
|
| 414 | ||
| 415 | /** |
|
| 416 | * Smallworld_sanitize(array(array) ) |
|