Reputation: 1228
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?
Upvotes: 89
Views: 57922
Reputation: 99
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...
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
Upvotes: 3
Reputation: 21
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"
Upvotes: 2
Reputation: 799082
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.
Upvotes: 69
Reputation: 46401
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.
Upvotes: 15
Reputation: 17030
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).
Upvotes: 19
Reputation: 1600
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:
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.
Upvotes: 70