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

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..

$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..


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.



Comments (0)

E-mail (Will not appear online)
;-) :-) :-D :-( :-o :-O B-) :oops: :-[] :-P
To prevent automated Bots form spamming, please enter the text you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
This comment form is powered by GentleSource Comment Script. It can be included in PHP or HTML files and allows visitors to leave comments on the website.

PHP Scripts   Throwaway Email   Throwaway Email