Replacing file_get_contents function with curl. Alternate for file_get_contents

Hi all

If your server does not support the file_get_contents function for php and it does not allow the url access this is the shortest and best alternate.

//$dataResponse = file_get_contents($request_url); // Error Check
//$data = json_decode($dataResponse);

$dataResponse =$this->curl_file_get_contents($request_url);
$data = json_decode($dataResponse);

function curl_file_get_contents($request)
$curl_req = curl_init($request);

curl_setopt($curl_req, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl_req, CURLOPT_HEADER, FALSE);

$contents = curl_exec($curl_req);


return $contents;
}///end of functn curl File get contents

the above function will work exactly same as file content url


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s