PHP怎樣執行一個動態網頁到緩衝中再產生靜態頁呢

來源:互聯網
上載者:User
產生靜態頁

在ASP中,一般用XMLHTTP對象的open方法開啟一個動態網頁進行執行,則這個動態網頁執行後產生的靜態html代碼可用一個adodb.stream對象儲存起來,再寫入到文字檔中。

在PHP中,可以用ob_start 開啟緩衝,讓字串先寫入到緩衝中,再將緩衝中的內容寫入到文字檔中,問題是,PHP怎麼執行一個動態網頁呢,在有些文章裡說,用header("location:adminnews.php");去執行一個動態網頁,但我試了,這樣他只會轉到這個動態網頁,並不會把這個動態網頁的內容輸出到緩衝中,My Code是這樣的,我想把adminnews.php的執行結果(產生的html代碼)全部寫入到jb51.html中,應該怎麼做啊
ob_start(); //開啟緩衝區
echo "Hello\n"; //輸出
header("location:adminnews.php"); //把瀏覽器重新導向到index.php

$cacheStr=ob_get_contents();
$handle=fopen("jb51.html","w");
fwrite($handle, $cacheStr);
ob_clean();
?>


回複討論(解決方案)

ob_start(); //開啟緩衝區
echo "Hello\n"; //輸出
include("adminnews.php"); //載入並運行

$cacheStr=ob_get_contents();
$handle=fopen("jb51.html","w");
fwrite($handle, $cacheStr);
ob_clean();

用$wstr=file_get_contents('http://www.baidu.com'); 這個函數去執行一個URL地址就可以了

file_get_contents 是php內建的函數嗎?

file_get_contents 是php內建的函數嗎?

是的 ,參看 file_get_contents

file_get_contents 是php內建的函數嗎?
是的,這些可以自己參考手冊的

file_get_contents即可擷取adminnews.php執行的結果。

file_get_contents即可擷取adminnews.php執行的結果。

我試了不行啊,用這個擷取到的是adminnews.php的原始碼,而不是adminnews.php的執行結果,我要的是執行後產生的html代碼,而不是原始的php代碼啊。。
ob_start(); //開啟緩衝區
echo "Hello\n"; //輸出
$cacheStr=file_get_contents("adminnews.php"); //

$handle=fopen("jb51.html","w");
fwrite($handle, $cacheStr);
ob_clean();
?>

file_get_contents即可擷取adminnews.php執行的結果。

我試了不行啊,用這個擷取到的是adminnews.php的原始碼,而不是adminnews.php的執行結果,我要的是執行後產生的html代碼,而不是原始的php代碼啊。。
ob_start(); //開啟緩衝區
echo "Hello\n"; //輸出
$cacheStr=file_get_contents("adminnews.php"); //

$handle=fopen("jb51.html","w");
fwrite($handle, $cacheStr);
ob_clean();
?>

你通過瀏覽器是怎麼訪問的,file_get_contents裡面就填寫那個地址

file_get_contents即可擷取adminnews.php執行的結果。

我試了不行啊,用這個擷取到的是adminnews.php的原始碼,而不是adminnews.php的執行結果,我要的是執行後產生的html代碼,而不是原始的php代碼啊。。
ob_start(); //開啟緩衝區
echo "Hello\n"; //輸出
$cacheStr=file_get_contents("adminnews.php"); //

$handle=fopen("jb51.html","w");
fwrite($handle, $cacheStr);
ob_clean();
?>

我給的例子那麼明顯了,還不懂?

我知道了,adminnews.php要寫成絕對路徑,唯寫檔案名稱是不會執行的

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.