What is SAPI and when would you use it?
Asked Answered
F

6

87

I've been learning about error handling in PHP recently and came across the error_log() function.

In the PHP manual, it talks about all the error log types and I understand all of them except for type 3 which states that the error message is sent directly to the SAPI logging handler. My question is what exactly is SAPI and when would you want to use it?

Forethought answered 30/3, 2012 at 17:50 Comment(0)
B
70

SAPI stands for "Server API" (and API stands for "Application Programming Interface"). It is the mechanism that controls the interaction between the "outside world" and the PHP/Zend engine. So, you would always want to use it. In fact, you cannot avoid using it without a lot of effort since even CLI is considered a SAPI.

Broch answered 30/3, 2012 at 17:54 Comment(3)
+0, Needs elaboration.Balfour
so how do you find out what your SAPI logger is on a running configuration neatly assembled by your package maintainer? (in order to make it log the messages you desperately want to see :).)Dorothadorothea
@n611x007: With a simple test script. If in doubt ask the system engineer who setup the system, verify the acceptance criteria for the configuration with her and then demand the test protocol of the current configuration revision for review. Then confirm the acceptance criteria from your end. No need to make things less straight forward.Verein
A
66

SAPI ( Server Application Programming Interface ) also know as ISAPI ( Internet Server Application Programming Interface) for Microsoft, NSAPI (Netscape Server Application Programming Interface) for Netscape.

API meaning.

For web developer, you can think of API such as REST, SOAP. You call a link you get a data from server. It allows you interact with the web server.

SAPI is different with REST or SOAP, SAPI is API (contract) used for server.

For example: Common Gateway Interface is an SAPI. If a web server support CGI and another executable program implement it so web server can inteface and generate web pages dynamically.

Look the picture below:

SAPI apache and php

mod_php implement an interface which apache and php can understand each other.

So what is SAPI exactly: It is a contract between Server (any kind of server) and the program. Just follow the contract and they don't need to know other side details.

Ase answered 30/7, 2016 at 3:6 Comment(3)
Can you tell me where this picture is taken from? I'm looking for some material on the topic with exactly this kind of graphical illustration.Altruist
I don't remember exactly. After some searching the old thing. I find one: slideshare.net/do_aki/php-and-sapi-and-zendengine2-and. Hope that help.Ase
I would like to add that it's possible to find the SAPI during runtime in php. You can use php_sapi_name() for it or the PHP_SAPI constant. Link: php.net/php_sapi_name. The SAPI is also displayed in the output of phpinfo(). The "cPanel" web hosting control panel calls the SAPI a 'PHP Handler'. Link: documentation.cpanel.net/display/EA4/PHP+HandlersMinium
C
19

From Wikipedia:

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.

As an example, PHP has a direct module interface called SAPI for different web servers; in case of PHP 5 and Apache 2.0 on Windows, it is provided in form of a DLL file called php5apache2.dll, which is a module that, among other functions, provides an interface between PHP and the web server, implemented in a form that the server understands. This form is what is known as a SAPI.

There are different kinds of SAPIs for various web server extensions. For example, another two SAPIs for the PHP language are Common Gateway Interface (CGI) and command-line interface (CLI).

Champignon answered 14/6, 2015 at 21:9 Comment(0)
P
14

For PHP available SAPIs are: Apache2 (mod_php), FPM, CGI, FastCGI, and CLI.

Arguable if API runs on the server it may be called SAPI.

Let me remind that FPM (FastCGI Process Manager) is very close to PHP FastCGI implementation with some additional features (mostly) useful for heavy-loaded sites.

Today, from the perspective of speed and efficiency FPM would be the most evolved SAPI. Apache or Nginx will perform better comparing the other mentioned SAPIs.

Platelet answered 22/8, 2018 at 20:54 Comment(5)
When used as the sapi_name argument for phpquery, it is case sensitive, so "FPM" returns: "Invalid SAPI (FPM) specified", while "fpm" works.Downrange
You say: "Apache or Nginx will perform better comparing the other mentioned SAPIs", but this still needs a SAPI to interpret PHP, so it's a bit ambiguous. And I don't really understand the difference between SAPI and CGI, it's like something invented by PHP?Tierney
It was meant Apache or Nginx under FPM will perform better etc.Platelet
@Platelet you should then edit your own answer to clarify that :-) While I personally agree (and always use FPM under nginx for that reason) there are a few edge cases where Apache2 + mod_php actually performs better than FPM, but these are rare and only useful in a very limited number of cases... which I don't clearly remember :) (The theory is that if you run PHP inside Apache, you can benefit from its 'closeness', and communications between the PHP interpreter and Apache itself are done via shared memory — way faster than any other alternatives — but I cannot confirm this documentally.)Ensheathe
@Tierney one might argue that the CGI is restricted to a single communications transport protocol, namely, via stdin/stdout between the server and the spawned client (e. g. a PHP interpreter instance); while SAPI can generically be applied to all forms of communications, such as pipes, named pipes, FIFOs, Unix sockets, Internet sockets, shared memory, external KVP servers, whatever. And, of course, stdin/stdout as well (since it is a valid method of communication!).Ensheathe
P
2

i am not an expert, but as far as i understood...

sapi is the channel through which the http server software communicates with cgi interpreter, that spawns an execution context (a shell, socket or port), read passed environment variables, read specified file (specified through environment variables, like SCRIPT_NAME), interprets the file and returns generated html code back to server to be delivered to http client (i.e. a browser)

in case of php, sapi could be...

  • a shell/cmd instance, in case of cgi (php-cgi or php.cgi.exe in windows)
  • a unix socket or pipe, in case of fastcgi (php-cgi or php[.exe])
  • a tcp or unix socket, in case of fpm (php-fpm)
  • some other channel i dont know

so for example, php error_log('my msg', 4) will send the message back to webserver through "stderr" stream of current sapi, the webserver software will catch the message and will display in its own error log facility (/var/log/nginx/error.log file in my case) as thrown by his cgi interpreter

i am not sure, correct me if i am wrong

Predation answered 5/3, 2023 at 22:35 Comment(2)
AFAIK you're absolutely correct :-)Ensheathe
You could also mention that CGI essentially just connects two processes in a parent-child relationship with stdin and stdout connected between both — a very simple and effective way of communicating without requiring a complex setup or the reliance on any sort of pre-existing communications protocol (e.g. Unix/Internet sockets, pipes, FIFOs, whatever). Most contemporary operating systems can have processes spawn child processes in some way and interconnect their stdin with stdout, thus the 'primitive' approach of CGI is universal and OS-independent.Ensheathe
M
1

In general, PHP applications very rarely need to know their SAPI. This usually matters if the script is to be called from a cron, but not from a web browser, in which case it checks that it's SAPI equals "cli"

Milner answered 13/11, 2022 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.