How does php switch the running mode from fpm-fcgi to cli?

Source: Internet
Author: User
When you use a php crawler, you are prompted to use the cli environment. I checked the current php version information as follows: I printed PHP_SAPI to show how fpm-fcgi can be changed to the cli server environment. linux + nginx is added: can I understand that cli must be in the command line mode? Just in the command...

When using a php crawler, you are prompted to use the cli environment.
I checked the current php version information as follows:

I printPHP_SAPIDisplayfpm-fcgi

How can it becomecli

The server environment is linux + nginx

Supplement: can I understand that cli must be in the command line mode?
I just tested it in the command line.PHP_SAPIOutputcli
Q: Can I implement cli through an http simulated call?

Reply content:

When using a php crawler, you are prompted to use the cli environment.
I checked the current php version information as follows:

I printPHP_SAPIDisplayfpm-fcgi

How can it becomecli

The server environment is linux + nginx

Supplement: can I understand that cli must be in the command line mode?
I just tested it in the command line.PHP_SAPIOutputcli
Q: Can I implement cli through an http simulated call?

CLI = command-line interface = command line interface

You can use popen or proc_open in php-fpm to call php-cli asynchronously to execute a php script, for example:

Example 1 (popen ):
  Array ('pipele', 'r'), // stdin (use fwrite to write data to the pipeline) 1 => array ('pipele', 'w '), // stdout (use stream_get_contents to retrieve pipeline output) 2 => array ('pipe', 'w'), // stderr (use stream_get_contents to obtain pipeline output) // 2 => array ('file', '/tmp/err.txt', 'A') // stderr (written to a file), $ pipes, // pipe (stdin/stdout/stderr) '/tmp', // Current PHP process working directory array ('foo' => 'bar') // php. in ini configuration, variables_order = "EGPCS", E indicates $ _ ENV; otherwise, $ _ ENV output is null); // var_dump ($ pipes); // resource of type (stream) if (is_resource ($ proc) {// stdin $ stdin = serialize (array ('time' => time (); fwrite ($ pipes [0], $ stdin); // transmits parameters to the script task. php fclose ($ pipes [0]); // after fclose closes the pipeline, proc_close can exit the sub-process. Otherwise, the register_shutdown_function (function () use ($ pipes, $ proc) will occur) {// event-driven (script end event), asynchronous callback // stdout $ stdout = stream_get_contents ($ pipes [1]); fclose ($ pipes [1]); // stderr $ stderr = stream_get_contents ($ pipes [2]); fclose ($ pipes [2]); // exit code (return the process termination status code, if an error occurs, return-1) $ status = proc_close ($ proc); $ data = array ('stdout' => $ stdout, 'stderr' => $ stderr, 'status' => $ status,); var_export ($ data); // echo json_encode ($ data) ;}}} foo (); // output: array ('stdout' => 'A: 1: {s: 4: "time"; s: 19: "21:26:29 ";}', 'stderr' => '', 'status' => 0 ,)

Cli is the command line, and linux isphpThis command. Php can execute command lines in many ways.execFor synchronous execution of functions, there are methods for asynchronous execution. let's take a look at the document in detail.

Run the script directly on the terminal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.