Replacing file_get_contents and fopen with Curl

Some of the web hosts don’t support some of the main file handling functions like fopen and file_get_contents. In this post I’m sharing another way of fetching contents of a file or a web address. I’m talking about Curl which has become most popular PHP function for the purpose of crawling and getting contents of any web page or a file. Its usage is simple and similar to fopen and file_get_contents.

Replacing file_get_contents and fopen with Curl


This simple PHP function will do all the work. CURL settings can be modified. All CURL settings start with curl_setopt. You can see that in below code.



Above code will output whatever it find on the requested page URL. And replacing fopen and file_get_contents  is very easy. Just copy the above curl_file_get_contents PHP function in any file where you want to use CURL and replace fopen and file_get_contents. Here is an example.
This is how you can replace fopen and file_get_contents with CURL.

 Complete Code


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top