Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
PHP 動(dòng)態(tài)頁(yè)面轉(zhuǎn)換為靜態(tài) HTML 頁(yè)面的主要意義在于提高網(wǎng)站的性能和可用性。下面是一些具體的好處:
然而,將 PHP 動(dòng)態(tài)頁(yè)面轉(zhuǎn)換為靜態(tài) HTML 頁(yè)面并不適用于所有類(lèi)型的網(wǎng)站,特別是對(duì)于需要?jiǎng)討B(tài)生成內(nèi)容和實(shí)時(shí)更新的網(wǎng)站,如電子商務(wù)網(wǎng)站、新聞網(wǎng)站等,這種轉(zhuǎn)換可能會(huì)導(dǎo)致信息更新不及時(shí),從而影響網(wǎng)站的可用性和用戶(hù)體驗(yàn)。因此,需要根據(jù)實(shí)際需求和情況來(lái)選擇是否需要將 PHP 動(dòng)態(tài)頁(yè)面轉(zhuǎn)換為靜態(tài) HTML 頁(yè)面。
要生成 HTML 文件,可以使用 PHP 中的文件處理函數(shù)和輸出函數(shù)來(lái)創(chuàng)建一個(gè)新的 HTML 文件并將其寫(xiě)入磁盤(pán)。下面是一個(gè)簡(jiǎn)單的示例:
<?php
// 創(chuàng)建一個(gè)新的 HTML 文件
$html = '<html><head><title>My HTML File</title></head><body><p>Hello, world!</p></body></html>';
$file = fopen('myhtmlfile.html', 'w');
// 將 HTML 內(nèi)容寫(xiě)入文件
fwrite($file, $html);
// 關(guān)閉文件
fclose($file);
// 輸出成功信息
echo 'HTML 文件已經(jīng)生成!';
?>
在上面的示例中,首先創(chuàng)建了一個(gè)包含 HTML 代碼的字符串變量 $html。然后使用 fopen()函數(shù)創(chuàng)建一個(gè)新的文件,并將其設(shè)置為寫(xiě)入模式 'w'。然后使用 fwrite() 函數(shù)將 HTML 內(nèi)容寫(xiě)入文件。最后使用 fclose() 函數(shù)關(guān)閉文件。生成的文件名為 myhtmlfile.html,它將保存在與 PHP 腳本相同的目錄中。
請(qǐng)注意,上述示例是一個(gè)簡(jiǎn)單的示例,用于說(shuō)明如何生成 HTML 文件。在實(shí)際應(yīng)用中,您可能需要更復(fù)雜的 HTML 代碼,需要使用循環(huán)或條件語(yǔ)句生成動(dòng)態(tài)內(nèi)容,并使用樣式表或 JavaScript 對(duì) HTML 進(jìn)行美化和交互。
將 PHP 動(dòng)態(tài)頁(yè)面進(jìn)行 HTML 緩存可以顯著提高網(wǎng)站性能,因?yàn)樗梢詼p少服務(wù)器請(qǐng)求和頁(yè)面加載時(shí)間。以下是一些用于將 PHP 動(dòng)態(tài)頁(yè)面進(jìn)行 HTML 緩存的常見(jiàn)方法:
1.使用 PHP 自帶的輸出緩存機(jī)制 ob_start() 和 ob_get_contents() 函數(shù)。
<?php
// 啟動(dòng)輸出緩存
ob_start();
// 輸出動(dòng)態(tài)內(nèi)容
echo 'Hello, world!';
// 將輸出內(nèi)容保存到緩存文件
file_put_contents('cached_page.html', ob_get_contents());
// 清空輸出緩存并關(guān)閉
ob_end_clean();
?>
在上述示例中,使用 ob_start() 函數(shù)啟動(dòng) PHP 輸出緩存,然后使用 echo 輸出動(dòng)態(tài)內(nèi)容。然后,使用 ob_get_contents() 函數(shù)將輸出內(nèi)容保存到緩存文件中,并使用 file_put_contents() 函數(shù)寫(xiě)入緩存文件。最后,使用 ob_end_clean() 函數(shù)清空輸出緩存并關(guān)閉。
2.使用第三方緩存庫(kù)如 Memcached 或 Redis。
這些緩存庫(kù)可以在服務(wù)器內(nèi)存中存儲(chǔ)緩存頁(yè)面,并且能夠通過(guò)一個(gè)唯一的鍵名來(lái)訪(fǎng)問(wèn)緩存頁(yè)面,從而加快頁(yè)面訪(fǎng)問(wèn)速度。具體實(shí)現(xiàn)可以參考對(duì)應(yīng)的文檔或者庫(kù)的使用說(shuō)明。
3.使用 HTTP 緩存頭信息。
這是一種在瀏覽器和服務(wù)器之間緩存頁(yè)面的機(jī)制,通過(guò)設(shè)置響應(yīng)頭信息來(lái)控制頁(yè)面緩存。可以通過(guò)設(shè)置 Expires、Cache-Control、Last-Modified 和 ETag 等響應(yīng)頭信息來(lái)控制頁(yè)面的緩存,從而減少對(duì)服務(wù)器的請(qǐng)求。具體實(shí)現(xiàn)可以參考 HTTP 協(xié)議規(guī)范或者使用對(duì)應(yīng)的 PHP 框架提供的緩存機(jī)制。
無(wú)論使用哪種方法,都需要謹(jǐn)慎考慮緩存時(shí)間和更新機(jī)制,以確保緩存頁(yè)面在需要時(shí)被及時(shí)更新,并避免緩存過(guò)期或出現(xiàn)臟數(shù)據(jù)等問(wèn)題。
AJAX 用于創(chuàng)造動(dòng)態(tài)性更強(qiáng)的應(yīng)用程序。
AJAX ASP/PHP 實(shí)例
下面的例子將為您演示當(dāng)用戶(hù)在輸入框中鍵入字符時(shí),網(wǎng)頁(yè)如何與 web 服務(wù)器進(jìn)行通信: 請(qǐng)?jiān)谙旅娴妮斎肟蛑墟I入字母(A - Z):
實(shí)例
Start typing a name in the input field below:
First name:
Suggestions:
實(shí)例解析 - showHint() 函數(shù)
當(dāng)用戶(hù)在上面的輸入框中鍵入字符時(shí),會(huì)執(zhí)行函數(shù) "showHint()" 。該函數(shù)由 "onkeyup" 事件觸發(fā):
function showHint(str)
{
var xmlhttp;
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint.html?q="+str,true);
xmlhttp.send();
}
源代碼解析:
如果輸入框?yàn)榭?(str.length==0),則該函數(shù)清空 txtHint 占位符的內(nèi)容,并退出函數(shù)。
如果輸入框不為空,showHint() 函數(shù)執(zhí)行以下任務(wù):
創(chuàng)建 XMLHttpRequest 對(duì)象
當(dāng)服務(wù)器響應(yīng)就緒時(shí)執(zhí)行函數(shù)
把請(qǐng)求發(fā)送到服務(wù)器上的文件
請(qǐng)注意我們向 URL 添加了一個(gè)參數(shù) q (帶有輸入框的內(nèi)容)
AJAX 服務(wù)器頁(yè)面 - ASP 和 PHP
由上面的 JavaScript 調(diào)用的服務(wù)器頁(yè)面是 ASP 文件,名為 "gethint.asp"。
下面,我們創(chuàng)建了兩個(gè)版本的服務(wù)器文件,一個(gè)用 ASP 編寫(xiě),另一個(gè)用 PHP 編寫(xiě)。
ASP 文件
"gethint.asp" 中的源代碼會(huì)檢查一個(gè)名字?jǐn)?shù)組,然后向?yàn)g覽器返回相應(yīng)的名字:
<%
response.expires=-1
dim a(30)
'Fill up array with names
a(1)="Anna"
a(2)="Brittany"
a(3)="Cinderella"
a(4)="Diana"
a(5)="Eva"
a(6)="Fiona"
a(7)="Gunda"
a(8)="Hege"
a(9)="Inga"
a(10)="Johanna"
a(11)="Kitty"
a(12)="Linda"
a(13)="Nina"
a(14)="Ophelia"
a(15)="Petunia"
a(16)="Amanda"
a(17)="Raquel"
a(18)="Cindy"
a(19)="Doris"
a(20)="Eve"
a(21)="Evita"
a(22)="Sunniva"
a(23)="Tove"
a(24)="Unni"
a(25)="Violet"
a(26)="Liza"
a(27)="Elizabeth"
a(28)="Ellen"
a(29)="Wenche"
a(30)="Vicky"
'get the q parameter from URL
q=ucase(request.querystring("q"))
'lookup all hints from array if length of q>0
if len(q)>0 then
hint=""
for i=1 to 30
if q=ucase(mid(a(i),1,len(q))) then
if hint="" then
hint=a(i)
else
hint=hint & " , " & a(i)
end if
end if
next
end if
'Output "no suggestion" if no hint were found
'or output the correct values
if hint="" then
response.write("no suggestion")
else
response.write(hint)
end if
%>
PHP 文件
下面的代碼用 PHP 編寫(xiě),與上面的 ASP 代碼作用是一樣的。
<?php
// Fill up array with names
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
如您還有不明白的可以在下面與我留言或是與我探討QQ群308855039,我們一起飛!
起因:因?yàn)楣居龅桨l(fā)稿問(wèn)題,很多人喜歡用word編碼,然后再發(fā)布到網(wǎng)站上。PHP的包中雖然有部分可以使用的類(lèi)庫(kù),但是對(duì)于圖片始終處理不好,我就想到了python。研究了下,python將word轉(zhuǎn)為html還真是方便。但是,怎么結(jié)合到服務(wù)器上呢?我們的服務(wù)器是用PHP開(kāi)發(fā)的。
1:python腳本
#!/usr/bin/python# -*- coding: UTF-8 -*-import sysfrom pydocx import PyDocXreload(sys)sys.setdefaultencoding('utf8')FileName = sys.argv[1] #獲取文件名參數(shù)ShortName = sys.argv[2] #獲取文件名參數(shù)html = PyDocX.to_html(FileName) # f = open("/www/wwwroot/micuer.com/pythoncode/runtime/99.txt", 'w') #服務(wù)器的全路徑# f.write(html)# f.close()print(html)
2:php處理腳本
public function uploadword(){ try { $file = request()->file("file"); // 上傳到本地服務(wù)器 $savename = \think\facade\Filesystem::disk('upload')->putFile( 'word', $file); $shotrname = time().".txt"; // 短名稱(chēng) $savename = "/www/wwwroot/micuer.com/data/upload/".$savename; //Request::domain(). $python_file_name = "/www/wwwroot/micuer.com/pythoncode/WordToHtml.py"; //組裝命令 $cmd = "python {$python_file_name} ".$savename." {$shotrname} 2>error.txt 2>&1"; $res = exec($cmd,$array, $ret); return json(["code"=>200,"msg"=>"成功","data"=>$savename,"cmd"=>$cmd,"array"=>$array]); } catch (think\exception\ValidateException $e) { return json(["code"=>40000,"msg"=>$e->getMessage()]); } }
上傳界面如下:
實(shí)現(xiàn)的功能就是利用PHP的exec函數(shù),調(diào)用py腳本,將html代碼返回給前臺(tái)服務(wù)器。
返回?cái)?shù)據(jù)如下
其實(shí),再處理這個(gè)方案中,也遇到了很多問(wèn)題,比如在命令行下只能成功,但是exec函數(shù)執(zhí)行不成功等等。
參考了資料:https://my.oschina.net/u/4427610/blog/3155816
也就是
exec("python python_test.py 2>error.txt 2>&1", $array, $ret);
在bash中0,1,2三個(gè)數(shù)字分代表STDIN_FILENO、STDOUT_FILENO、STDERR_FILENO,即標(biāo)準(zhǔn)輸入(一般是鍵盤(pán)),標(biāo)準(zhǔn)輸出(一般是顯示屏,準(zhǔn)確的說(shuō)是用戶(hù)終端控制臺(tái)),標(biāo)準(zhǔn)錯(cuò)誤(出錯(cuò)信息輸出)。
也可以通過(guò)以下方式將標(biāo)準(zhǔn)錯(cuò)誤重定向到標(biāo)準(zhǔn)輸出保存到$array中:
打印之后,發(fā)現(xiàn)是沒(méi)有權(quán)限調(diào)用。于是就直接改為輸出了,也就是 py的print(html)函數(shù)。
注意幾點(diǎn):
1:執(zhí)行權(quán)限問(wèn)題
2:exec(“python python_test.py 2>error.txt 2>&1”, $array, $ret); 中 $array就接受到了 print(html)的值
3:各個(gè)腳本盡量使用全路徑
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。