1

According to Wikipedia's definition:

In other words, SAPI is actually an application programming interface (API) provided by the web server to help other developers in extending the web server capabilities.

Different kinds of SAPIs exist for various web-server extensions. For example, in addition to those listed above, other SAPIs for the PHP language include the Common Gateway Interface (CGI) and command-line interface (CLI).

But, i think when running a php file by terminal(command line), or using php-cli interactive shell only zend engine will be involved to interpret the code.

Is there something that I'm wrong about or don't know?

Can it be because of interacting with client through the web server?



Edit: Further explanations for a better understanding of the subject in addition to the accepted answer:

Advanced PHP Programming By George Schlossnagle

enter image description here

The outermost layer, where PHP interacts with other applications, is the Server Abstraction API (SAPI) layer. The SAPI layer partially handles the startup and shutdown of PHP inside an application, and it provides hooks for handling data such as cookies and POST data in an application-agnostic manner.

Below the SAPI layer lies the PHP engine itself. The core PHP code handles setting up the running environment (populating global variables and setting default .ini options), providing interfaces such as the stream's I/O interface, parsing of data, and most importantly, providing an interface for loading extensions (both statically compiled extensions and dynamically loaded extensions).

At the core of PHP lies the Zend Engine, which we have discussed in depth here. As you've seen, the Zend Engine fully handles the parsing and execution of scripts. The Zend Engine was also designed for extensibility and allows for entirely overriding its basic functionality (compilation, execution, and error handling), overriding selective portions of its behavior (overriding op_handlers in particular ops), and having functions called on registerable hooks (on every function call, on every opcode, and so on). These features allow for easy integration of caches, profilers, debuggers, and semantics-altering extensions.

The SAPI Layer

The SAPI layer is the abstraction layer that allows for easy embedding of PHP into other applications. Some SAPIs include the following:

  • mod_php5 This is the PHP module for Apache, and it is a SAPI that embeds PHP into the Apache Web server.

  • fastcgi This is an implementation of FastCGI that provides a scalable extension to the CGI standard. FastCGI is a persistent CGI daemon that can handle multiple requests. FastCGI is the preferred method of running PHP under IIS and shows performance almost as good as that of mod_php5.

  • CLI This is the standalone interpreter for running PHP scripts from the command line, and it is a thin wrapper around a SAPI layer.

  • embed This is a general-purpose SAPI that is designed to provide a C library interface for embedding a PHP interpreter in an arbitrary application.

The idea is that regardless of the application, PHP needs to communicate with an application in a number of common places, so the SAPI interface provides a hook for each of those places. When an application needs to start up PHP, for instance, it calls the startup hook. Conversely, when PHP wants to output information, it uses the provided ub_write hook, which the SAPI layer author has coded to use the correct output method for the application PHP is running in.

Read more

Community
  • 1
  • 1
msoa
  • 1,149
  • 3
  • 11
  • 30
  • 1
    The linked duplicate only explains **what** SAPI is, and none of the answers address the OP's concern: "**Why** is PHP CLI considered as a kind of SAPI?" Although this question might attract opinionated answers, it can surely be addressed on a factual basis as well. Voting to reopen. – skomisa Apr 12 '20 at 17:57
  • What's your exact question? "Only zend engine" sounds a bit weird here, as this is used on all PHP requests, both on CLI and using a server – Nico Haase Apr 12 '20 at 19:51
  • And what is wrong or unclear about https://stackoverflow.com/questions/9948008/what-is-sapi-and-when-would-you-use-it? – Nico Haase Apr 12 '20 at 19:55
  • @NicoHaase The main question is the title of the ask: Why is PHP CLI considered as a kind of SAPI? About ‍`zend engine` you're right. I see it as an interpreter for PHP language. Out of the web server discussion, we must able to interact with the PHP interpreter by a way, so i think `PHP CLI` is used for this mean. If a user just wants to play with the PHP via the command line, do you know another way? Does the `PHP CLI interactive shell` is also a kind of SAPI? If so, what is your reason? – msoa Apr 12 '20 at 21:38
  • 1
    @NicoHaase There is nothing "wrong" with the question you link to, but it clearly does not address the question being asked here. In fact the accepted answer for that question states "_CLI is considered a SAPI_" but does not explain why. Some _"why..."_ questions on SO are not answerable, or not answerable without inviting opinionated answers, but I don't think this particular question falls into either of those categories. – skomisa Apr 13 '20 at 05:42
  • [1] I have removed the **SAPI** tag from your question since that is for Microsoft's Speech API. [2] I suspect that PHP CLI is _"considered as a kind of SAPI"_ because it can optionally be run as a limited web server: "[As of PHP 5.4.0, **the CLI SAPI provides a built-in web server**](https://www.php.net/manual/en/features.commandline.webserver.php)". Is this what you were suggesting yourself, in the final sentence of your question? – skomisa Apr 13 '20 at 15:08
  • @skomisa I think [**SAPI**](https://en.wikipedia.org/wiki/Server_Application_Programming_Interface) is true, because it can be also considered as [**Server Application Programming Interface**](https://en.wikipedia.org/wiki/SAPI) – msoa Apr 13 '20 at 20:57
  • @skomisa About `built-in web server`, no, i don't mean that. my mean is about some interactions with client. like when we want to use Multi-Threading in PHP. [Document](http://www.php.net/manual/en/intro.pthreads.php): `Warning: The pthreads extension cannot be used in a web server environment. Threading in PHP should therefore remain to CLI-based applications only.` – msoa Apr 13 '20 at 21:34
  • @skomisa i add explanations in the editing my question. also you're right about the `SAPI` tag, it's in conflict with **Microsoft's Speech API**. – msoa Apr 13 '20 at 22:44

1 Answers1

3

"Why" is always a slippery, somewhat philosophical, question; ultimately, the answer to "why is CLI considered a SAPI" is "because that's how the developers defined it". If they'd called it "CLI mode", would you still have asked "why"?

But you do ask a more concrete question, which can be paraphrased as:

When running a program on the CLI, why do you need a SAPI as well as the Zend Engine?

Or even more succinctly:

What does the CLI SAPI do?

The Zend Engine on its own takes a series of instructions, and executes them. It can manage variables, arithmetic, function definitions and calls, class definitions, and so on. But none of those are very useful if you can't get any output from the program; and most commonly you want to provide some variable input as well.

Some forms of input and output are based only on the operating system you're running on, not the SAPI - reading or writing a file based on an absolute path, or accessing something over a network connection. You could theoretically have a running mode that only gave access to those, but it would feel very limited. (Would that still be a "SAPI"? Definitely a philosophical question.)

Consider something as commonplace as echo: there's no absolute definition of "output" that the Zend Engine can manage directly. In a web server context, you expect echo to add some output to the HTTP response the server will send the client; in a command-line context, you expect it to show the output in the console where you ran the command, or be "piped" to another command if you run something like php foo.php | grep error.

The CLI SAPI doesn't provide all the same facilities that a web server SAPI would, but it fills a similar role in interfacing your program, running in the Zend Engine, to the outside world. Here are a few of the things it needs to do:

  • Attach output to the parent console or "standard output" stream
  • Make the "standard input" and "standard error" streams available
  • Populate $argv and $argc based on the arguments the script was invoked with
  • Populate $_ENV with the environment variables the process inherited
  • Define an initial value for "current working directory" for use with relative file paths
IMSoP
  • 65,743
  • 7
  • 83
  • 127
  • Very Thanks, your descriptions is very useful and i read it several times. You're right, if developers called it other thing, this question may not have arisen ;). For more understanding, I think it's needed to dig deeper on how to `CLI SAPI` is implemented. – msoa Apr 13 '20 at 09:54