开发者

How to extract the content of a specific div from another page on the same server?

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 techn开发者_运维问答ical 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.


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.


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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜