/**
* 本地静态化生成养老院详情页(替代 html_yanglaoyuanxx 中的 cURL 自请求)
* 使用 ob_start + include_once 在进程内渲染,避免公网带宽消耗
* @param int $yid 养老院ID
* @return string|false 成功返回生成的HTML相对路径,失败返回false
*/
function html_yanglaoyuanxx_local($yid)
{
$yid = verify_id($yid);
if (!$yid) {
return false;
}
$strsql = "Select `yid`,`staticposttime`,`htmlurl`,`yhtmlurl`,`CompanyName` From `vte_yanglaoyuan` Where `yid`='$yid' And `Isopen`='1'";
$result = sql_queryone($strsql);
if (!$result['yid']) {
return false;
}
$finalposttime = time();
sql_idu("Update `vte_yanglaoyuan` Set `finalposttime`='{$finalposttime}' Where `yid`='{$yid}' And `Isopen`=1");
$dis_htmlurl = $result['htmlurl'];
$dis_yhtmlurl = $result['yhtmlurl'];
$dis_CompanyName = $result['CompanyName'];
$rand = random_number(10);
$newpagename = "y{$yid}.html";
$shop_root = DetectPhysicalPath();
// ========== 1. PC端静态化 ==========
$pc_script = $shop_root . 'createseting/yanglaoyuan/yanglaoyuanxx.php';
$pc_content = _render_page_local($pc_script, array(
'yid' => $yid,
'pagetype' => 'create_html',
'rand' => $rand,
));
if ($pc_content === false || strlen($pc_content) < 1000) {
// 内容过短或渲染失败,记录日志便于排查
error_log("[html_yanglaoyuanxx_local] PC render failed or too short for yid={$yid}, len=" . ($pc_content === false ? 'FALSE' : strlen($pc_content)));
return false;
}
$pc_dir = 'ylyxx/';
$pc_dir_full = $shop_root . rtrim($pc_dir, '/');
mk_dir($pc_dir_full);
$pc_file = $pc_dir_full . '/' . $newpagename;
$w_len = file_put_contents($pc_file, $pc_content);
if ($w_len < 1000) {
error_log("[html_yanglaoyuanxx_local] PC write failed for yid={$yid}");
return false;
}
$htmlurl = "{$pc_dir}{$newpagename}";
sql_idu("Update `vte_yanglaoyuan` Set `htmlurl`='{$htmlurl}' Where `yid`='{$yid}' And `Isopen`=1");
// ========== 2. 移动端静态化 ==========
$mobile_script = $shop_root . 'mobileweb/createseting/yanglaoyuan/yanglaoyuanxx/index.php';
$mobile_content = _render_page_local($mobile_script, array(
'yid' => $yid,
'pagetype' => 'create_html',
'rand' => $rand,
));
if ($mobile_content !== false && strlen($mobile_content) >= 1000) {
$mobile_dir = 'mobileweb/ylyxx/';
$mobile_dir_full = $shop_root . rtrim($mobile_dir, '/');
mk_dir($mobile_dir_full);
$mobile_file = $mobile_dir_full . '/' . $newpagename;
file_put_contents($mobile_file, $mobile_content);
} else {
error_log("[html_yanglaoyuanxx_local] Mobile render failed or too short for yid={$yid}");
}
// ========== 3. 301跳转页处理 ==========
if ($dis_htmlurl && $dis_yhtmlurl) {
yanglaoyuanxx301($htmlurl, $dis_yhtmlurl, $dis_CompanyName, 'pc');
yanglaoyuanxx301($htmlurl, $dis_yhtmlurl, $dis_CompanyName, 'web');
}
return $htmlurl;
}
/**
* 内部辅助:在进程内渲染PHP页面并捕获输出
* 通过设置 $_GET 参数 + ob_start + include_once 模拟HTTP请求
* @param string $script_path 脚本绝对路径
* @param array $get_params 模拟的GET参数
* @return string|false 渲染输出的HTML内容,失败返回false
*/
function _render_page_local($script_path, $get_params = array())
{
if (!file_exists($script_path)) {
error_log("[_render_page_local] Script not found: {$script_path}");
return false;
}
// 保存原始 $_GET
$original_get = $_GET;
// 注入模拟参数
foreach ($get_params as $k => $v) {
$_GET[$k] = $v;
}
// 确保 session 已启动(避免渲染脚本中 session_start() 报警告)
if (function_exists('session_status') && session_status() === PHP_SESSION_NONE) {
@session_start();
} elseif (!isset($_SESSION)) {
@session_start();
}
ob_start();
$render_ok = true;
try {
include_once $script_path;
} catch (\Throwable $e) {
error_log("[_render_page_local] Exception in {$script_path}: " . $e->getMessage());
$render_ok = false;
}
$content = ob_get_clean();
// 恢复原始 $_GET
$_GET = $original_get;
if (!$render_ok || $content === false || strlen(trim($content)) === 0) {
return false;
}
return $content;
}