用于网页的文件重定向

时间:2016-03-29 06:27:16

标签: php

我想保存在php中生成的网页的html代码。我正在尝试的基本想法是:

ob_start (); $buffered = true;

    fclose (STDOUT);
    STDOUT = fopen ("Test/test.htm","wb");

    $site_name = 'Cory'; 
    chdir ('..');
    include ($site_name.'_V'.$v_defs["sv"].'/Begin.php'); // Draws website

    fclose (STDOUT);

我已经尝试了所有我能想到的fopen命令的变体,但我总是得到一个解析错误。

1 个答案:

答案 0 :(得分:1)

我使用此函数来获取页面的外部html:

function get_inner_html( $node ) {
    $innerHTML= '';
    $children = $node->childNodes;
    foreach ($children as $child) {
        $innerHTML .= $child->ownerDocument->saveXML( $child );
    }

    return $innerHTML;
} 
function get_html_table($link,$element,$class){
//$data=date("Y-m-d");
$html = file_get_contents($link); //get the html returned from the following url
$poke_doc = new DOMDocument();
libxml_use_internal_errors(false); //disable libxml errors
libxml_use_internal_errors(true);
if(!empty($html)){ //if any html is actually returned
    $poke_doc->loadHTML($html);
    libxml_clear_errors(); //remove errors for yucky html
    $poke_xpath = new DOMXPath($poke_doc);
    $poke_type = $poke_xpath->query("//".$element."[@class='".$class."']");
    $table = "<table>";
    foreach($poke_type as $type){
        $table .= get_inner_html($type);
    }
    $table .= "</table>";
return $table;
}

第2步:

echo get_html_table('https://www.link.com','table','class');
//first parameter is the link,second: type of dom element, last is the class
$content=get_html_table('https://www.link.com','table','class');

之后,您可以将$ content保存到文件中