Ways to Import External Content Into Pages
There are a number of ways to insert often-updated content into a page, content obtained from an external file or URL. Here, you will find out how to do it with PHP.
The content generally would be something that is updated frequently. Perhaps the special of the day, a poem, a news item, the latest blog post, a photograph of the employee of the week, etc.
Content being imported needs to be plain text with HTML markup for images, video, and other content — just like the source code of a web page.
Generally, the imported content would have HTML markup. Text without HTML markup would publish as a run-in block of text — unless the content is inserted within a pre
container or in a div
container that has a CSS white-space:pre-wrap;
declaration.
Most methods addressed here are for content that gets imported and published directly. But there is also a way to import content that contains PHP code to be run at the web page after the content has been imported.
Let's get started.
Importing With readfile(…)
This is probably the easiest method of all. Here is the code:
<?php readfile("{$_SERVER['DOCUMENT_ROOT']}/directory/file.html"); ?>
Replace /directory/file.html
with the location of the file to be imported into your page and you are good to go. (The location of the file is its URL minus the HTTP(S) and domain name part. The location for http://example.com/directory/file.html
is /directory/file.html
.)
Depending on your PHP INI file's allow_url_fopen setting, you might be able to import content with a URL.
Give it a try. If yes, it would give you the option of pulling in content from a different domain — an extremely handy option when you want to publish the same content on web pages at more than one domain.
Here is the code:
<?php readfile('https://your_domain.com/directory/file.html'); ?>
Replace https://your_domain.com/directory/file.html
with the URL of the file to be imported into your page. Give it a try and see if it works on your server.
Importing With echo(file_get_contents(…))
This is similar to the readfile() method except readfile() sends the content to the web page directly and file_get_contents() only reads the file. With file_get_contents(), the echo() function needs to be used to send the content to the web page.
Because that is the only difference that affects your use of them, the file_get_contents() section that follows is essentially the same as it was for readfile().
Here is the file_get_contents() format:
<?php echo(file_get_contents("{$_SERVER['DOCUMENT_ROOT']}/directory/file.html")); ?>
Replace /directory/file.html
with the location of the file to be imported into your page.
As with readfile(), you may be able to use a URL instead of a file location.
Here is the code:
<?php echo(file_get_contents('https://your_domain.com/directory/file.html')); ?>
Replace https://your_domain.com/directory/file.html
with the URL of the file to be imported into your page.
Importing With include(…)
When imported content contains PHP code, use this method.
One of the include() family of PHP functions — include(), include_once(), require(), require_once() — needs to be included if the remote content contains PHP code that must be run at the web page after it has been imported.
Here is the code:
<?php include("{$_SERVER['DOCUMENT_ROOT']}/directory/file.php"); ?>
Replace /directory/file.php
with the location of the file to be imported into your page. Any valid PHP code in the content will run before the content is sent to the browser.
Depending on your PHP INI file's allow_url_include setting, you might be able to use include() to import content with a URL.
Here is the code:
<?php include('https://your_domain.com/directory/file.php'); ?>
Replace https://your_domain.com/directory/file.php
with the URL of the file to be imported into your page.
To use the other include() family of PHP functions — include_once(), require(), require_once() — instead of include() in the above code examples, replace include()
with the preferred function. (The PHP documentation for include() applies to, and has links to, each of this family of functions.)
When You Need But Can't Specify a URL
As mentioned, the PHP INI file is where the settings are that let you use a URL in the above-mentioned functions, instead of a server file location.
Unless you have your own server or a Virtual Private Server (VPS), you are unlikely to be able to update that INI file. You're stuck with what the hosting company set up — often because the same INI file is used by other hosting accounts.
There is a work-around.
When you can't use a URL in the functions mentioned earlier, it may still be possible to import content with a URL. It will require many lines of code. And it would require cURL to be part of your PHP (which most hosting companies provide for).
Here is the code.
<?php
$URL = "https://example.com/directory/file.html";
$options = array(
CURLOPT_RETURNTRANSFER => true, // Return web page
CURLOPT_HEADER => false, // Don't return headers
CURLOPT_CONNECTTIMEOUT => 120, // Timeout on connect
CURLOPT_TIMEOUT => 120, // Timeout on response
CURLOPT_FOLLOWLOCATION => true, // Follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_VERBOSE => false
);
$ch = curl_init($URL);
curl_setopt_array($ch,$options);
echo(curl_exec($ch));
curl_close($ch);
?>
Replace https://example.com/directory/file.html
with the URL of the content to import. It should pull the content right into your page at the location where you put the PHP code.
Information about the options in the above code, and a list of other options that may be specified, are at the PHP cURL options page.
Pulling in Content
You now have 3 one-line methods to pull in content and publish it on a web page. The one-line methods can be used to pull in content from the same domain as the web page by specifying the file location and, with many hosting accounts, from other domains by specifying a URL.
One of those one-line methods can be used to insert content into the page, content that has PHP code to run before being published on the page.
There is also a multi-line method for pulling in content by specifying a URL that can be used if none of the one-line methods work with a URL at your hosting account.
For practice, try all methods. It can give you a good idea about the advantages and disadvantages of each.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager