我有一個腳本我工作的一個小問題,這就是我遇到:保存PHP代碼文件中使用的fwrite
首先一點關於如何腳本作品。它實際上使用WordPress(它是一個插件),它根據可在後端更改的各種設置生成動態頁面。我將添加一個導出到HTML功能,他們可以在插件中創建頁面後創建該頁面的靜態版本。
在這個導出的頁面中,我需要它將一個PHP函數保存在文件的頂部,另一個保存在頁面的其他地方。這就是我想要做的事:
$fbscript=file_get_contents('fbscript.txt');
$newcontent=str_replace('<!-- dont erase this line -->',$fbscript,$newcontent);
$fbscript2=file_get_contents('fbscript2.txt');
$newcontent=str_replace('<!-- dont erase this line 2 -->',$fbscript2,$newcontent);
的2個dont erase this line
東西都在它需要把腳本動態頁面的某個地方。這就是某處出在出口上一條:
<br />
<b>Notice</b>: Undefined index: signed_request in <b>C:\xampp\htdocs\wp\wp-content\plugins\easyfanpagedesign\default.theme\htmlpost-31345.php</b> on line <b>82</b><br />
<br />
<b>Notice</b>: Undefined offset: 1 in <b>C:\xampp\htdocs\wp\wp-content\plugins\easyfanpagedesign\default.theme\htmlpost-31345.php</b> on line <b>3</b><br />
所以我想我真正想要問的是我如何使用fwrite的一個.php
擴展名保存文件,並有內部的一個PHP腳本保存的文件。這是PHP腳本我想添加到頁面使用FWRITE(Facebook的PHP SDK)保存的例子:
<?php
function parsePageSignedRequesttt($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
return null;
}
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
return null;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
?>
這是我該做的一切整個export.php文件:
<?php
$content=$_POST['efpd_page_content'];
$uistyle=$_POST['efpd_ui_style'];
$app_id=$_POST['efpd_appid'];
$the_fscripts=$_POST['efpd_fscripts'];
$the_hscripts=$_POST['efpd_hscripts'];
$bgstuff=$_POST['efpd_bgstuff'];
$jquery=$_POST['efpd_jquery'];
$jqueryui=$_POST['efpd_jqueryui'];
$cycle=$_POST['efpd_cycle'];
$copytext=$_POST['efpd_copytext'];
$affstr=$_POST['efpd_affstr'];
$the_style=$_POST['efpd_style'];
$the_gwf=$_POST['efpd_gwfstyle'];
$secret=$_POST['efpd_secret'];
if(empty($secret)){$secret=999999;}
$newcontent=file_get_contents($_POST['efpd_refurl']);
$fbscript=file_get_contents('fbscript.txt');
$newcontent=str_replace('<!-- dont erase this line -->',$fbscript,$newcontent);
$fbscript2=file_get_contents('fbscript2.txt');
$newcontent=str_replace('<!-- dont erase this line 2 -->',$fbscript2,$newcontent);
$newcontent=str_replace('THE_SECRET',$secret,$newcontent);
//die(var_dump($newcontent));
$int=rand(1,99999);
$savefile = "htmlpost-$int.php";
$handle = fopen($savefile, 'w') or die("can't open file");
fwrite($handle,$newcontent);
fclose($handle);
echo "<a href='$savefile'>Right Click > Save As to download the generated HTML page.</a>";
?>
代碼中的某處必須是'$ _REQUEST ['signed_request']',對不對? – Niko
是的,這是在頁面中間添加的第二個腳本(fbscript2.txt) – Jared
好吧,這就是導致這些錯誤出現的原因 - 因爲該頁面似乎旨在成爲Facebook上的應用程序和你不要真的在那裏運行它(現在呢?)。 – Niko