Using cURL in Place of Include() or Require() – PHP
In this write-up, we’ll be going through the benefits of using cURL in place of require or include. If you are on a shared host that doesn’t allow you to change the settings within your php.ini file, then you may be restricted from using the include or require functions. Most of the time, cURL is enabled even if these others aren’t.
Usually, when you include a php script within your page, you use something like
<?php include_once('/path/to/file.php'); ?>
However, some shared hosts will now allow you to do this. A work-around, assuming the cURL library is enabled (check phpinfo()) is
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "/path/to/file.php"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); ?>
Be sure to change the “/path/to/file.php” line to point to the file that you would like to include.
What this code does is “include” the file.php that we have just set as our CURLOPT_URL. This is a very simple, efficient way of including external files in a page, without the need for an iframe, include() (include_once() as well), or require().