Centeralized News Source

6 posts by 3 authors in: Forums > CMS Builder
Last Post: March 4, 2014   (RSS)

By Ryan - February 25, 2014

Hi All,

I've four websites (Website A,B,C & D) all using CMS Builder.

Site A is the parent site and it's the only one with a news editor in the CMS.

My question is, is it possible to display news from website A on the other 3 websites.

It seems like the news page on website B, C and D would need two connections one for it's own CMS and one for Website A.

I could use an iframe, but was looking for something a bit cleaner.

Has anyone any ideas on this?

By Chris - February 25, 2014

How about fetching a news page from Website A and injecting its HTML straight into your pages on Websites B, C, and D?

<?php echo file_get_contents('http://websiteA.com/news_include.php'); ?>

Hope this helps!

All the best,
Chris

Hi Ryan,

Are the sites hosted on the same server? If so, it might be possible to call the viewer functions of site A from sites B, C and D, and have access to its getRecords function.

Do sites B, C and D have access to the MySQL database of site A? If so you could just manually create a MySQL call to get the data you require:

http://www.w3schools.com/php/php_mysql_select.asp

If the sites are on different hosting services, and there isn't a way to share the data directly, you could create a mini "API" to share the data from site A to sites B, C and D. 

On site A, you could use the getRecords function to share your news feed, and json encode it. Here's a quick example I made of the API system:

<?php

//Set the header as a json object, as this is what our API is going to output.
header('Content-type: application/json');
  
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/greg/www/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records from 'news'
list($carsRecords, $carsMetaData) = getRecords(array(
  'tableName'   => 'cars',
  'perPage'     => '10',
  'loadUploads' => true,
  'allowSearch' => false,
));


//Prepare the data for being output as a single array
$finalArray = array('data' => $carsRecords, 'meta' => $carsMetaData);


//display the final array as a json object
echo json_encode($finalArray);

So we're using the standard getRecords function on site A to retrieve our data, but instead of displaying it as HTML, we're displaying it as a JSON object that can easily be retrieved and read by another site. 

Then on Sites B, C and D, we need to create a system to retrieve this data and display it on the page. The best way to do this is to use the getPage function which is built into CMS Builder:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/home/greg/www/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  
  //Pass any variables that are set in the URL (for example page number, search filters, etc) and turn them into a URL string.
  $string = http_build_query($_REQUEST);


  /*
  *  You'll need to put the address of the page that's creating our json object on site A as the first variable in the getPage function, the $string variable ensures that 
  *  any variables on the current site are passed to site A. getPage returns several variables, the first of which should be the json object.
  */
  list($html, $httpStatusCode, $header, $request) = getPage('http://localhost/test.php?'.$string, 3);

  
  //If we receive anything but a 200 code, then something's gone wrong sending the data from site A. So we stop processing and display an error.
  if($httpStatusCode != 200){ die ("The news page on site A is currently down, please come back later!"); }

  //Convert the json object sent by site A into a PHP array.
  $returnedData = json_decode($html, true);

  //Recreate the original variables from the getRecords function of site A
  $carsRecords = $returnedData['data'];
  $carsMetaData = $returnedData['meta'];

  //The showme function will just display the data from the array, but you can now use the data as you would with a normal getRecords function. 
  showme($carsRecords);
  showme($carsMetaData);

Hopefully the notes in the code above explain what's happening at each step clearly. 

The only potential issue with this system is that sites B,C and D are going to have to wait for the data to be sent from site A, which will increase page load times. Also, if site A goes down, then the news pages for sites B, C and D will go down as well.

Let me know if you have any questions!

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Chris - February 26, 2014

> I would like to be able to alter the where statement on each site locally.

Another option would be to use three separate includes on website A:

On website B:

<?php echo file_get_contents('http://websiteA.com/news_include_B.php'); ?>

On website C:

<?php echo file_get_contents('http://websiteA.com/news_include_C.php'); ?>

On website D:

<?php echo file_get_contents('http://websiteA.com/news_include_D.php'); ?>

All the best,
Chris

By Ryan - March 4, 2014

Thanks Chris / Greg, In the end I set up a new SQL connection manually and pulled the record across from the parent website.