I wrote a function which runs a php script and returns its output. I wish to know if there’s a better way to do this, as this seems like more of a workaround to me. Since the script is local, it seems rather wasteful to use page_get_contents (with a full URL) as that takes more time.
function runpage($path, $query) {
parse_str($query, $_GET);
$oldquery = $_SERVER["QUERY_STRING"];
$_SERVER["QUERY_STRING"] = $query;
ob_start();
include $path;
$out = ob_get_contents();
ob_end_clean();
$_SERVER["QUERY_STRING"] = $oldquery;
return $out;
}
Thanks much!
Yes, it is a kludge. No, there isn’t a significantly better way.
The whole point of that snippet is to be a workaround. You could very well rewrite the included script, make it read it’s input variables from another array and have it return the output correctly over a function call. Or you could turn it into an executable script, and access
argvinstead of$_GET– but that would require the same amount of restructuring.Yes, it’s awkward. But get over it. Shell scripts and pipes are by no means cleaner than this PHP include (apart from the $_GET override it’s similar to templating anyhow). And regardless of that, awkward doesn’t mean it’s going to fail. Just don’t make this a regular construct.