2016-03-29 63 views
-1

我想保存在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

我用這個函數來獲取頁面的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'); 

您可以在$內容保存到一個文件中