开发者

Launching a local PHP Script from a PHP Script and passing GET Parameters

开发者 https://www.devze.com 2023-02-02 17:53 出处:网络
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

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 ra开发者_Python百科ther 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 argv instead 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消