📜 ⬆️ ⬇️

We use php-cgi in our applications

PHP is one of the most common programming languages. Created for writing small home pages, it has gradually grown and is now used by millions of websites. However, business is not limited to websites - it can be used in almost any application, the benefit is interactive, CGI and FastCGI modes.
CGI mode I want to describe. Its advantages are in relative simplicity and the possibility of transferring various data (including binary) to scripts. He has only one minus - the speed of work, due to repeated launches of the application. However, this minus is corrected with the help of the newer FastCGI protocol.

First, let's define what the Common Gateway Interface (CGI) is. In fact, this is just a launch of an application with certain environment variables. Since this is still a web technology, then the variables will be closely related to the HTTP protocol. Let's look at RFC 3875 and see what environmental variables are needed for CGI / 1.1:

For POST and PUT requests, the request data is transferred to the standard input of the application.
However, to run php-cgi, all these variables are only desirable, but not necessary. A mandatory environment variable is only one, and even then not specified in the RFC - SCRIPT_FILENAME. It should contain the path to the PHP script in the file system, for example "/home/some-user/htdocs/test.php". To run a PHP script through CGI in your application, you just need to specify it and run php-cgi (not “php”, namely “php-cgi”!), Redirecting its standard input and output.
If you do not write your web server, you need to trim the output PHP headers. Intercepting the output, wait until the first empty line appears (just two line breaks in a row - "\ n \ n") and use only what comes after it.
PHP in CGI mode is desirable to use only when it is necessary not to actively work with scripts, passing them any data or variables. If you need active work, it is better to use FastCGI, and if you don’t need to pass anything to the script, then it will be easier to start the PHP interpreter, passing the script path to it as an argument.
And not to be unsubstantiated, a small example of work (Windows, C #):
using System; using System.Diagnostics; namespace PhpApp { class Program { static void Main(string[] args) { Process php = new Process(); //   php-cgi php.StartInfo.FileName = "c:\\php\\php-cgi.exe"; //  ShellExecute   -     php.StartInfo.UseShellExecute = false; // ,      php.StartInfo.RedirectStandardOutput = true; //          php.OutputDataReceived += new DataReceivedEventHandler(php_OutputDataReceived); //     php-cgi   php.StartInfo.EnvironmentVariables.Add("SCRIPT_FILENAME", "test.php"); //   php.Start(); //        php.BeginOutputReadLine(); // ,  php-cgi   php.WaitForExit(); //    php.Close(); //    Console.ReadKey(); } static void php_OutputDataReceived(object sender, DataReceivedEventArgs e) { //     ( ),    if (e.Data == null) return; //     Console.WriteLine(e.Data); } } } 


')

Source: https://habr.com/ru/post/121885/


All Articles