2012-11-06 24 views
0

我试图找出是否有可能&使用什么样的代码:加载当前页面的内容和回声出一个相对路径到一个特定的页面(c.html)这是瓦特/ “#navbar一个” 使用PHP或PHP Simple Html DOM Parser.PHP获取当前页面的具体内容

到目前为止我的代码:

<?php 
$pg = 'c.html'; 
include_once '%resource(simple_html_dom.php)%'; 
/* $cpath = $_SERVER['REQUEST_URI']; Old version */ // Path to current pg from root 
$cpath = "http://www.".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
echo var_dump($cpath).": Current Root Path"."<br />"; // "http://www.partiproductions.com/copyr/index.php" - Correct 
$cfile = basename($cpath); 
echo 'Current File: ' . $cfile . "<br />"; // "index.php" - Current pg, Correct 

$html = file_get_html($cpath); // Getting Warning: URL file-access is disabled in the server configuration & failed to open stream: no suitable wrapper could be found in.. & Fatal error: Call to a member function find() on a non-object in... 
foreach($html->find(sprintf('#navbar a[href=%s]', $pg)) as $path) { 
    echo 'Path: ' . $path."<br />"; 
} 
?> 
+0

做'的print_r($ _ SERVER)',看看是否有任何值那里,你可以使用。 – Landon

+0

尝试'$ html = file_get_html('index.php');'看看你是否有任何东西。如果没有,是否与您的脚本在同一个目录中是'index.php'?如果没有,请尝试获取'$ cpath'。 – cpilko

+0

嗨@cpilko试图只是index.php,但没有奏效。 index.php是当前页面。在 – parti

回答

1

你的主要问题是与您的来电file_get_html($ CFile的)。在你的榜样

$的CFile将包含类似/copyr/index.php

当你通过这个进file_get_html(),它要寻找你的服务器的根目录的目录/ copyr,和里面有一个index.php文件。根据您所指出的警告,您实际上并未在服务器的根目录中拥有此文件夹结构。

你真正需要做的是包括在URI前面的完整URL您当前拥有,这样的:

$cpath = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 

这将导致在这样的路径:http://www.yourserver.com/copyr/index.php,你应该再努力for file_get_html();

+0

好的Wally,我用http:// www。在开始时,它现在输出当前路径为http:// www.partiproductions.com/copyr/index.php,但它是一个字符串 - 会导致问题吗?这个php脚本在当前页面上,因为我没有明确说明。获取:警告:file_get_contents()[function.file-get-contents]:文件名不能为空... – parti

+0

使用cpath获取:Warning:file_get_contents()[function.file-get-contents]:URL文件访问是在服务器配置中禁用.. AND file_get_contents(http://www.partiproductions.com/copyr/index.php)[function.file-get-contents]:未能打开流:没有找到合适的包装器。 – parti

0

基于从提问者的更新信息,我会按照不同的方法。

创建仅包含要跨这两个文件共享内容的新文件。然后,在这两个文件中(或更高版本,如果需要)使用include()函数从新的共享内容文件注入内容。

index.php文件:

<?php 
//Any require PHP code goes here 
?> 
<html> 
    <body> 
    <?php include('sharedfile.php');?> 
    </body> 
</html> 

/copyr/c.php文件:

<?php 
//Any require PHP code goes here 
?> 
<html> 
    <body> 
    <?php include('../sharedfile.php');?> 
    </body> 
</html> 

sharedfile.php:

// You need to close the PHP tag before echoing HTML content 
?> 
<p> 
    This content is displayed via include() both on index.php and /copyr/c.php 
</p> 
<?php // The PHP tag needs to be re-opened at the end of your shared file 

钍这里的好处是,您现在可以通过遵循相同的技术,在您网站上的任何文件中使用sharedfile.php文件内容。您也不需要解析页面的DOM,只需删除要在多个页面上显示的内容,这些内容可能很慢并且容易出错。

+0

现在让我们留在w/just index&c.html/php文件中。在c文件中,我希望能够定位#myid或.myclass并将特定内容导入到index.php中。 c.php的路径需要自动找到。在你的方法中,有一个文件w /只有内容需要,我不能做 - 必须从一个更大的现有文件(c.html/php) - 拉谢谢 – parti