Deru Knowledgebase
Search:     Advanced search
Browse by category:
Contact Us

How you replace your php fopen function with curl?

Add comment
Views: 2304
Votes: 0
Comments: 0
Posted: 11 Aug, 2009
by: Kanchan C.
Updated: 24 Jul, 2012
by: Joseph S.
Now a days the fopen() function is disabled from all of the servers for security reason.

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.


<?php
 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.


<?php

$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.
Others in this Category
document My web pages aren't being updated when I upload...
document How do I setup and use SSH?
document How do I block an IP address using Cpanel?
document How do I backup & restore a MySQL database using phpMyAdmin?
document How to redirect your site form domain-name.com to www.domain-name.com?
document How to Upgrade Wordpress Manually?
document Setting Default Page with Plesk
document How do I create a MySQL database in Plesk?
document How to reset password in Wordpress
document Cpanel showing incorrect disk usage
document Album access issue in Gallery
document Error :: Access denied; you need the SUPER privilege for this operation.
document How to install Cpanel Proxy
document How to view mail header in different email clients
document How to make hosting secure
document How to view the bandwidth details of the domain via cPanel?
document How to use Awstats, webalizer, Analog to view the traffic details of the domain?
document How to improve wordpress performance?
document How to reset the Joomla admin password
document How to reset the Magento admin password
document How to reset the Drupal admin password
document How to reset the Expression Engine admin password?
document How to reset email account password in webmin control panel?
document How to set up email forwarder using usermin?
document How to Exclude unwanted Files from Backup in cPanel !!
document How to enable webmail in the email account, if it is disabled?
document Bandwidth reset after migration in a cPanel server
document How to view the traffic usage statistics using Plesk control panel?
document How to create database, FTP accounts in Plesk?
document 'hosts' file and its location in different OS
document How to optimize MySQL database using phpMyAdmin?
document How to restrict the number of simultaneous connections to a server in FileZilla?
document How do I enable hotlink protection?
document How to repair a broken database?



RSS