fetch url with minimum risks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bores
Forum Newbie
Posts: 3
Joined: Fri Feb 17, 2012 6:56 pm

fetch url with minimum risks

Post by Bores »

Hi

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;
}
Regards
Post Reply