67

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?

Chris
  • 93,263
  • 50
  • 204
  • 189
jjmorph
  • 948
  • 1
  • 6
  • 15

4 Answers4

54

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.

CreationTribe
  • 552
  • 1
  • 5
  • 16
Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
46

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.

Wolverine
  • 1,634
  • 1
  • 15
  • 17
christian Nguyen
  • 1,224
  • 11
  • 10
  • 4
    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. – dwytrykus Jan 07 '17 at 11:56
  • 2
    I don't remember exactly. After some searching the old thing. I find one: http://www.slideshare.net/do_aki/php-and-sapi-and-zendengine2-and. Hope that help. – christian Nguyen Jan 11 '17 at 01:35
  • 1
    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: http://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: https://documentation.cpanel.net/display/EA4/PHP+Handlers – Julian Jul 12 '18 at 07:25
16

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).

Community
  • 1
  • 1
numediaweb
  • 13,837
  • 11
  • 59
  • 100
9

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.

prosti
  • 27,149
  • 7
  • 127
  • 118
  • 1
    When used as the sapi_name argument for *phpquery*, it is case sensitive, so "FPM" returns: "Invalid SAPI (FPM) specified", while "fpm" works. – Free Radical Mar 25 '19 at 19:10
  • 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? – jcarlosweb Apr 07 '21 at 11:19
  • 1
    It was meant Apache or Nginx under FPM will perform better etc. – prosti Apr 07 '21 at 11:24
  • Thank you for the clarification – jcarlosweb Apr 07 '21 at 12:56