如何从另一台服务器获取一个html页面,并在该文件中的某个点插入php代码
We're completely redoing our website, and putting most of our pages into a new CMS (Adobe cq5- on server
1). However, we have some php/mysql pages that cant go in there, so we are housing them on our own server(server2). I need our pages on server2 to look exactly like the page on server1.
There is a template on Server 1, with a url of: http://staging-cms.com/content/directory/template.html
So, I need to
1.pull the template from Server1 using the above URL.
2.Look in that template for this div:
<div class="text"><p>CONTENT FOR TEMPLATE GOES HERE.<br></p></div>
3.Take the content of the page on server 2 and insert it at the above point. 4. Return the full page, with template and contents onto a page on server2.
So, the final url would be something like:
http://server2.com/books.php
Is this possible?
I've tried this below, but all I get is a page with databases.php printed.
$Content = file_get_contents("http://staging-cms.uc.edu/content/libraries /template.html");
$Content = str_ireplace('CONTENT FOR TEMPLATE GOES HERE','databases.php','What goes here??');//staging-cms.uc.edu/content/libraries/template.html);
print ($Content);`
I've tried other things where I can return the template (kind of). But I cant seem to get it all working together?
我们完全重做了我们的网站,并将大部分页面放入新的CMS(Adobe cq5-服务器上) p>
1)。 但是,我们有一些不能进入的php / mysql页面,因此我们将它们放在我们自己的服务器上(server2)。 我需要server2上的页面看起来与server1上的页面完全相同。 p>
服务器1上有一个模板,其网址为: http://staging-cms.com/content/directory/template.html p> 所以,我需要使用上面的URL从服务器1中删除模板。 2。在该模板中查找此div: p>
3.Take 服务器2上页面的内容并将其插入上述点。
4。 将包含模板和内容的整页返回到server2上的页面。 p>
因此,最终的URL将类似于:
这可能吗? p>
我在下面尝试了这个,但我得到的是一个带有
databases的页面。 php打印。 p>
我尝试了其他可以返回模板的东西(种类)。 但我似乎无法让所有人一起工作? p>
div>&lt; div class =“text”&gt;&lt; p&gt;模板的内容在这里。&lt; br&gt;&lt; / p&gt;&lt; / div&gt; code> p>
http://server2.com/books .php code> p>
$ Content = file_get_contents(“http://staging-cms.uc.edu/content/libraries /template.html");
ConContent = str_ireplace('TEMPLATE GOES HERE','databases.php','What goes here ??'); // staging-cms.uc.edu/content/libraries/template.html);
print($ Content) ;`
code> pre>
$template = file_get_contents("http://staging-cms.uc.edu/content/libraries/template.html");
list($top, $bottom) = explode('CONTENT FOR TEMPLATE GOES HERE', $template);
echo $top;
include '/path/to/databases.php';
echo $bottom;
The PHP code in databases must actually be executed before displaying to the user, so you can't use str_ireplace(), this deals with literal strings only, not file names or PHP code.
Instead you need to split the template into 2 parts (top and bottom), and then include
the PHP code which will be executed.
I highly recommmend explode();
function. If you have have the exact text "CONTENT FOR TEMPLATE GOES HERE." your code will be something like this.
<?php
$Content = file_get_contents("http://template.url");
$PHPFileContent = file_get_contents("/path/to/databases.php");
$explodedContent = explode("CONTENT FOR TEMPLATE GOES HERE", $Content);
$theFinalContent = "$explodedContent[0] $PHPFileContent $explodedContent[1]";
?>
You need to search how explode() works btw.
Hope helps.
One Good Design Patters that i like to use is that:
$new_file = file_get_contents($file_for_future);
$old_file = file_get_contents($file_to_copy_body);
preg_match('\preg to find the right position\', $new_file,$match, PREG_OFFSET_CAPTURE);
reg pattern can be '\div class=\"text\">
\' but is untested, do it yourself.
$pos = $match[0][1];
The third and forth command is for find the right position that you want to put your new code inside the new file, is a INT number and should be the exact point that you want to start writing your old code.
PREG_OFFSET_CAPTURE in the preg_match force the regular expression to say where they find the standart.
And the magic is:
$final_file = substr($new_file, 0, $pos)." ".$old_file." ".substr($new_file, $pos);
This logic will generate a file, using the new template, inserting the old code right where you want to put it.