I am in need for a script able to fetch a url, some times file_get_contents($url) works but sometimes no, I need a script that tests if file_get_contents and fsockopen are presents or not and accordingly, use the appropriete one without throwing any exeption. To simplify that for you I show you a good usage of fsockopen here, so only what I need is the test instruction if()else...
Code: Select all
function loadXFile($location) {
$fp = fsockopen($location, 80, $errno, $errstr, 30);
if (!$fp) {
$result = "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: ".$location."\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp);
}
return $result;
}