/ Published in: PHP
this comes from YAPB plugin for WordPress. kudos to the author.
(there are some functions that are deprecated in PHP 5.2+ - you'll need to replace eregi with the mb_ version). This should be quite failsafe, regardless if curl is installed or the settings in your php.ini (allow_url_fopen), etc.
(there are some functions that are deprecated in PHP 5.2+ - you'll need to replace eregi with the mb_ version). This should be quite failsafe, regardless if curl is installed or the settings in your php.ini (allow_url_fopen), etc.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** * sicheres einlesen externer Dateien */ private function URLreadFsock($host, $file, &$errstr, $successonly=true, $port=80, $timeout=10) { $errstr = 'fsockopen() unavailable'; return false; } $out = 'GET '.$file.' HTTP/1.0'." "; $out .= 'Host: '.$host." "; $out .= 'Connection: Close'." "; $isHeader = true; $Data_header = ''; $Data_body = ''; $header_newlocation = ''; if ($isHeader) { $Data_header .= $line; } else { $Data_body .= $line; } $header_newlocation = $matches[1]; } if ($isHeader && ($line == " ")) { $isHeader = false; if ($successonly) { switch ($errno) { case 200: // great, continue break; default: $errstr = $errno.' '.$errstr.($header_newlocation ? '; Location: '.$header_newlocation : ''); return false; break; } } } } return $Data_body; } return null; } private function ParseURLbetter($url) { if (!@$parsedURL['port']) { case 'ftp': $parsedURL['port'] = 21; break; case 'https': $parsedURL['port'] = 443; break; case 'http': $parsedURL['port'] = 80; break; } } return $parsedURL; } private function _getVersionFile($url, &$error, $timeout=10, $followredirects=true) { $error = ''; $parsed_url = $this->ParseURLbetter($url); while (true) { $tryagain = false; $rawData = $this->URLreadFsock(@$parsed_url['host'], @$parsed_url['path'].'?'.@$parsed_url['query'], $errstr, true, (@$parsed_url['port'] ? @$parsed_url['port'] : 80), $timeout); if (!@$alreadyLookedAtURLs[$matches[1]]) { // loop through and examine new URL $error .= 'URL "'.$url.'" redirected to "'.$matches[1].'"'; $tryagain = true; $alreadyLookedAtURLs[$matches[1]] = true; $parsed_url = $this->ParseURLbetter($matches[1]); } } if (!$tryagain) { break; } } if ($rawData === false) { $error .= 'Error opening "'.$url.'":'."\n\n".$errstr; return false; } elseif ($rawData === null) { // fall through $error .= 'Error opening "'.$url.'":'."\n\n".$errstr; } else { } // curl } $error .= 'CURL available but returned no data; '; } else { $error .= 'CURL unavailable; '; } // fopen $rawData = ''; $error_fopen = ''; do { $rawData .= $buffer; } else { } $error .= $error_fopen; if (!$error_fopen) { } $error .= '; "allow_url_fopen" enabled but returned no data ('.$error_fopen.'); '; } else { $error .= '"allow_url_fopen" disabled; '; } return false; }