產生靜態頁
在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要寫成絕對路徑,唯寫檔案名稱是不會執行的