102017

Display text from another website using PHP and CURL Andrew Mallett | 3rd October 2017

Sometimes it's useful to grab some text and to replicate it in other places. For instance you might have a body of frequently-changing information for dissemination over several websites. Instead of replicating and uploading it to these various places, wouldn't it be useful to have this happen automatically?

This kind of thing is technically achievable through the use of PHP includes, however most servers disable PHP includes linking to other domains for security reasons.

The following code uses curl to grab some characters from a specified clear text file and display them within the body of the calling web page..

<?php
$curl = curl_init('http://andrewmallett.net/links.txt');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
$result = curl_exec($curl);
echo $result;
?>

I use this to replicate a set of weblinks across a number of sites. Each site points to the master text file, so that I only have to alter this one file and all the links change accordingly. Marvellous.

Although the the linked page is a .txt file, it contains HTML code which is picked up by curl and embedded into the containing .php pages..

http://andrewmallet.net
http://mallett.com.au

Note that the above code, links_grabber.php can be called and used as a PHP include:

<?PHP include('links_grabber.php'); ?>

Simply change the second line pointing to the url of the required information.

 

Back