[英]Get generated html content of php script on same server
我有一個php頁面,該頁面基於已解析的查詢字符串將html輸出到瀏覽器。 我遇到的問題是我需要通過php動態檢索此html源代碼。
以下代碼將不起作用,因為它試圖解析同一服務器上的絕對路徑:
$url = 'http://example.com/myScript.php';
$html = file_get_contents($url);
如果我手動設置絕對路徑,它將僅以文本形式返回php內容(不像瀏覽器那樣執行):
$url = '/dir1/dir2/dir3/dir4/myScript.php';
$html = file_get_contents($url);
然后,我對其進行了研究,發現使用ob_get_contents
可以起作用。 下面的代碼按預期工作,執行腳本並返回html輸出。
$url = '/dir1/dir2/dir3/dir4/myScript.php';
ob_start();
include($url);
$html = ob_get_contents();
ob_end_clean();
上述解決方案的問題在於,一旦我將查詢字符串放在末尾,它就會失敗。 我認為這是因為它將查詢字符串視為文件名的一部分。
使用PHP ob_get_contents
<?php
ob_start();
$original_get = $_GET;
$_GET = ["query" => "tags", "you" => "need", "in" => "file.php"];
$file = "file.php";
include($file);
$_GET = $original_get;
$content = ob_get_contents();
ob_clean();
echo $content;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.