Tuesday, August 11, 2009

How you replace your php fopen function with curl?



Now a days the fopen() function is disabled by almost all of the web hosting companies for security vulnerability.

If you want to use the remote file inclusion in your web page you have to replace the fopen() function code with curl. Here is the procedure.

Suppose here is an sample code using fopen. It will include the remote file content of the url http://www.someurl.com.

=============

if ($fp = fopen('http://www.someurl.com/', 'r')) {
$content = '';
// keep reading until there's nothing left
while ($line = fread($fp, 1024)) {
$content .= $line;
}
echo $content;
} else {
echo "error";
}
?>
=============

Now here is the replacement of the above code with curl.

=============


$url = "http://www.someurl.com/"

// initialize a new curl resource
$ch = curl_init();

// set the url to fetch
curl_setopt($ch, CURLOPT_URL, $url);

//excluding the headers
curl_setopt($ch, CURLOPT_HEADER, 0);

// return the value instead of printing the response to browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


//take the content as a instance
$file = curl_exec($ch);


//close the session and free all resources
curl_close($ch);
?>
=============


That's all.

No comments:

Post a Comment

 Linux Interview  Linux booting process EXT4 XFS file system runlevel scan disk hba driver systool -c fc_host lspci -nn | grep -i hba single...