1

Hello friends I am running 2 applications on the same server and I wanted to import some content of Application-1 into Application-2 and similarly some content of Application-2 into Application-2.

Currently this is how I am getting it:

<?php
    $app1 = file_get_contents('http://localhost/cms');

    file_put_contents('cms.html', $app1); 

    $app2 = file_get_contents('http://localhost/blog');

    file_put_contents('blog.html', $app2); 
?>

Like this I am saving these 2 files with the HTML of those applications

Now lets say I have

a <div id="header"> in cms.html which I want to import in <div id="header"> of Application-2's index.php file

and

a <div id="footer"> in blog.htmlwhich I want in <div id="footer"> of Application-1's index.php file.

How do I do this?

UPDATE : Both applications are Joomla and WordPress.

My Template is a simple one without any JavaScript so kindly suggest a PHP solution until and unless it is compulsory to load JavaScript, I want to keep it simple and light.

Please also note I have just started learning programing hence It will be very difficult for me to understand too technical a language hence requesting a simple language. Hope you understand.

If my approach is wrong at the very first place, then please feel free to suggest a better method of doing this task.

Kindly help.

UPDATE 2 : Please not my issue is not of getting content from the other application. My issue is to get a fragment of the HTML file that is being generated.

Vikram Rao
  • 1,068
  • 4
  • 24
  • 62

4 Answers4

0

Here's what I would suggest to you:

Create header.php and footer.php, each containing the content that you want to have in both of your headers / footers.

Inside your <div id="header">, add a <?php include "header.php" ?> in all your files which you want to share the same header. Likewise, do the same thing for the footer.

Unless you really have to, trying to parse through parts of another page to find the appropriate tags will make what you're trying to do unneccessarily complicated.

-- Edit -- Since you're working with CMSs, there aren't actually files sitting on your system to parse through. The HTML you see in a browser is created at the time someone requests it.

To do what you're describing, you could use php's cURL library to access the page, then parse through the html to pull out the tags you're looking for. However, this method will be extremely slow, since you're basically requiring the user to load 2 pages rather than 1.

You may want to look into the backend for joomla / wordpress - see how the footer is stored in the database, and put in code to access it from the other application.

Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
  • Hey Sam thanks for responding but nop that is not possible because the 2 applications are Joomla and WordPress and the page needs to be processed before I can get the output. So including merely the `header.php` or `footer.php` will not help. because these contain the module / widget positions which are processed by the application before the output. – Vikram Rao Jan 08 '11 at 18:43
  • @Vikram this is important information you should add to the question body. – Gordon Jan 08 '11 at 18:54
0

If the files reside on the same local server, there is no need to use file_get_contents with an HTTP URL. Doing so will do an HTTP request and that adds unneccessary latency.

Unless you want the files to be processed by their respective applications, you can simply load the contents of the HTML files from their file path, e.g.

$content = file_get_contents('/path/to/application1/html/file.htm');

If those files contain PHP that needs to be evaluated, use

include 'path/to/application1/html/file.htm';

instead. Note that using include will put PHP into HTML mode at the beginning of the target file, and resumes PHP mode again at the end. So you must enclose any PHP code in the file with the appropriate <?php open tag.

If you need to capture the output of the include call in a variable, wrap it into

ob_start();
include '…'; // this can also take a URL
$content = ob_get_clean();

This will enable output buffering.

If you need to work on the HTML inside the files, use a proper HTML parser.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Sorry for incomplete information. Actually both applications are Joomla and WordPress these DIVs that I am talking about contain the module / widget positions and they need to be processed by the applications before the output. – Vikram Rao Jan 08 '11 at 19:01
  • @Vikram I am still not sure I understand the question, but I guess including or using your current approach is the only feasible approach. Sorry if I cannot be any more helpful. – Gordon Jan 08 '11 at 19:08
  • By include method it is giving the error Fatal error: Using $this when not in object context in E:\xampp\htdocs\cms\templates\joomla\blocks\header.php on line 3 because in line 3 of header.php file we are calling it as `countModules('logo or navigation')) { ?>` and these are Joomla specific tags. – Vikram Rao Jan 08 '11 at 19:16
  • @Vikram, ok. That's probably due to how Joomla processes templates then. I'm not familiar with Joomla, but I'd assume it is using some sort of View Class that simply includes your template within a render method of the View instance, e.g. in object context. You could try to include and instantiate the View class, but I doubt this will be as painless as simply doing it the way you are already doing it. And if that works and is fast enough then I wouldn't bother. Making two completely separate apps interact is always a challenge. – Gordon Jan 08 '11 at 19:23
  • Hmmm right. But actually I wanted to extract the content of ` – Vikram Rao Jan 08 '11 at 20:26
  • @Vikram if you want to extract content from the HTML use any of the suggested parsers given in the last link of my answer. There is endless examples on StackOverflow on how to use DOM to extract parts of markup. Here is some of mine: http://stackoverflow.com/search?q=user%3A208809+dom – Gordon Jan 08 '11 at 20:29
  • Well never mind solved the issue. Thanks a lot for your help and patience in perusing the issue. – Vikram Rao Jan 08 '11 at 20:59
0

Well I guess I found a better way of doing it myself so I am posting here as an answer for the sake of others who are looking for a similar solution.

Write a class which will read between the markers and get that bit.

<?Php
class className {
    private $_content;
    private $_Top;
    private $_Bottom;

    public function __construct(
        $url, $markerStartTop = null, 
        $markerEndTop = null, 
        $markerStartBottom = null, 
        $markerEndBottom = null){

            $this->_content = file_get_contents($url);
            $this->_renderTop($markerStartTop, $markerEndTop);
            $this->_renderBottom($markerStartBottom, $markerEndBottom);
        }

    public function renderTop($markerStart = null, $markerEnd = null){
        return $this->_Top;
        }

    private function _renderTop($markerStart = null, $markerEnd = null){
        $markerStart = (is_null($markerStart)) ? '<!– Start Top Block –>' : (string)$markerStart;
        $markerEnd = (is_null($markerEnd)) ? '<!– End Top Block –>' : (string)$markerEnd;

        $TopStart = stripos($this->_content, $markerStart);
        $TopEnd = stripos($this->_content, $markerEnd);

        $this->_Top = substr($this->_content, $TopStart, $TopEnd-$TopStart);
        }

    public function renderBottom(){
        return $this->_Bottom;
        }

    private function _renderBottom($markerStart = null, $markerEnd = null){ 
        $markerStart = (is_null($markerStart)) ? '<!– Start Bottom Block –>' : (string)$markerStart;
        $markerEnd = (is_null($markerEnd)) ? '<!– End Bottom Block –>' : (string)$markerEnd;

        $BottomStart = stripos($this->_content, $markerStart);
        $BottomEnd = stripos($this->_content, $markerEnd);

        $this->_Bottom = substr($this->_content, $BottomStart, $BottomEnd-$BottomStart);
        }
}
?>

Call it:

<?php
    $parts = new className('url/to/the/application');
    echo $parts->renderTop();
?>

This method is fetching exactly the fragment of the content which you marked between the desired markers.

Vikram Rao
  • 1,068
  • 4
  • 24
  • 62
0

This assumes well-formed XHTML and PHP 5:

  1. Obtain the desired HTML via some method, e.g. a cURL request.
  2. Use SimpleXML to parse the XML into a DOM, then XPath to extract the desired nodes.
  3. Emit the extracted nodes as text.
Rob
  • 47,999
  • 5
  • 74
  • 91