Guides & Tutorials

Technology tutorials, technology guides and more

iLeno Logo

file(), file_get_contents(), fopen(), curl, fsockopen() and snoopy library

file()

$url = 'https://www.ileno.com';
// using file() function to get content
$lines_array = file($url);
// turn array into one variable
$lines_string=implode('',$lines_array);
// output, can be also saved locally on the server
echo $lines_string;

file_get_contents()

$url = 'https://www.ileno.com';
// file_get_contents() reads remote webpage content
$lines_string=file_get_contents($url);
// output, can be also saved locally on the server
echo htmlspecialchars($lines_string);
To use file_get_contents "allow_url_fopen = On" must be in php.ini file.

fopen()->fread()->fclose()

$url = 'https://www.ileno.com';
// fopen opens webpage in Binary
$handle = fopen($url,"rb");
// initialize
$lines_string = "";
// read content line by line
do {
	$data=fread($handle,1024);
	if (strlen($data)==0) {
		break;
	}
	$lines_string.=$data;
} while (true);
// close handle to release resources
fclose($handle);
// output, can be also saved locally on the server
echo $lines_string;
To use fopen "allow_url_fopen = On" must be in php.ini file.

curl

$url = 'https://www.ileno.com';
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// get URL content
$lines_string=curl_exec($ch);
// close handle to release resources
curl_close($ch);
// output, can be also saved locally on the server
echo $lines_string;
You need to have curl enabled to use it.

fsockopen() socket

$fp = fsockopen("t.qq.com", 80, $errno, $errstr, 30);
if (!$fp) {
	echo "$errstr ($errno)
n";
} else {
	$out = "GET / HTTP/1.1rn";
	$out .= "Host: www.ileno.comrn";
	$out .= "Connection: Closernrn";
	fwrite($fp, $out);
	while (!feof($fp)) {
		echo fgets($fp, 128);
	}
	fclose($fp);
}

snoopy library

// include snoopy library
require('Snoopy.class.php');
// initialize snoopy object
$snoopy = new Snoopy;
$url = "http://www.ileno.com";
// read webpage content
$snoopy->fetch($url);
// save it to $lines_string
$lines_string = $snoopy->results;
// output, can be also saved locally on the server
echo $lines_string;
Recently quite popular. It simulates a web browser from your server.