有没有一种从PHP脚本调用CGI脚本的好方法?

I saw that there is a virtual() function in PHP that will call a CGI script, but is that the best way? Can I pass any parameters to that scripts as well?

I saw some examples using file_get_contents() or include() and passing in the URL of a CGI script, but that feels like a hack.

Use exec() if you can call it locally. If it needs to be invoked as a CGI (as in the script is designed to only work within a CGI environment), then you'll need to call it via include() or file_get_contents(). virtual() will flush your buffers and append the output of the sub-request.

You can pass parameters through include(), file_get_contents(), and virtual() as GET parameters:

http://localhost/cgi-bin/foo?param1=val1&param2=val2

If possible, go the exec() route. Using the other methods may require a config change.

When using exec(), you'll need to pass the arguments like you would for any CLI program.

foo val1 val2
foo param1=val1 param2=val2

How you pass the parameters in will depend on how you want to parse them out later in the other program/script. They'll show up in the called program like they would if you called it from the command line.

I was having trouble with calling a CGI script from my php page too. Later I realized that the issue was that my page file had the .html suffix, rather than the .php suffix. Fixing that allowed my script to work for me. This is probably NOT your problem, but I thought I'd mention it just in case.