SAPI Layer of PHP

Posted on Updated on

SAPI Layer of PHP

The SAPI layer is the reflection layer that considers simple inserting of PHP into different applications. Some SAPIs incorporate the accompanying:

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

  • fastcgi This is a usage of FastCGI that gives an adaptable expansion to the CGI standard. FastCGI is an industrious CGI daemon that can deal with different solicitations. FastCGI is the favored strategy for running PHP under IIS and shows execution nearly on a par with that of mod_php5.
  • CLI This is the standalone translator for running PHP scripts from the charge line, and it is a thin wrapper around a SAPI layer.
  • embed This is a universally useful SAPI that is intended to give a C library interface to implanting a PHP mediator in a discretionary application.

The thought is that paying little mind to the application, PHP needs to speak with an application in various basic spots, so the SAPI interface gives a snare to each of those spots. At the point when an application needs to fire up PHP, for example, it calls the startup snare. Alternately, when PHP needs to yield data, it utilizes the gaveub_write snare, which the SAPI layer writer has coded to utilize the right yield strategy for the application PHP is running in.

To comprehend the capacities of the SAPI layer, it is most effortless to take a gander at the snares it executes. Each SAPI interface enlists the accompanying struct, with PHP depicting the callbacks it executes:

struct _sapi_module_struct {

roast *name;

roast *pretty_name;

int (*startup)(struct _sapi_module_struct *sapi_module);

int (*shutdown)(struct _sapi_module_struct *sapi_module);

int (*activate)(TSRMLS_D);

int (*deactivate)(TSRMLS_D);

int (*ub_write)(const roast *str, unsigned intstr_length TSRMLS_DC);

void (*flush)(void *server_context);

struct detail *(*get_stat)(TSRMLS_D);

roast *(*getenv)(char *name, size_tname_len TSRMLS_DC);

void (*sapi_error)(int sort, const roast *error_msg, …);

int (*header_handler)(sapi_header_struct *sapi_header,

sapi_headers_struct *sapi_headers TSRMLS_DC);

int (*send_headers)(sapi_headers_struct *sapi_headers TSRMLS_DC);

void (*send_header)(sapi_header_struct *sapi_header,

void *server_context TSRMLS_DC);

int (*read_post)(char *buffer, uintcount_bytes TSRMLS_DC);

roast *(*read_cookies)(TSRMLS_D);

void (*register_server_variables)(zval *track_vars_array TSRMLS_DC);

void (*log_message)(char *message);

roast *php_ini_path_override;

void (*block_interruptions)(void);

void (*unblock_interruptions)(void);

void (*default_post_reader)(TSRMLS_D);

void (*treat_data)(intarg, roast *str, zval *destArray TSRMLS_DC);

roast *executable_location;


int (*get_fd)(int *fd TSRMLS_DC);

int (*force_http_10)(TSRMLS_D);

int (*get_target_uid)(uid_t * TSRMLS_DC);

int (*get_target_gid)(gid_t * TSRMLS_DC);

unsignedint (*input_filter)(intarg, scorch *var,

scorch **val, unsigned intval_len TSRMLS_DC);

void (*ini_defaults)(HashTable *configuration_hash);



The accompanying are a portion of the remarkable components from this case:

  • startup This is known as the first run through the SAPI is introduced. In an application that will serve numerous solicitations, this is performed just once. For instance, in mod_php5, this is performed in the parent procedure before youngsters are forked.
  • activate This is called toward the start of every demand. It reinitializes all the per-ask for SAPI information structures.
  • deactivate This is called toward the end of every demand. It guarantees that the sum total of what information has been accurately flushed to the application, and afterward it pulverizes all the per-ask for information structures.
  • shutdown This is called at translator shutdown. It obliterates all the SAPI structures.
  • ub_write This is the thing that PHP will use to yield information to the customer. In the CLI SAPI, this is as straightforward as keeping in touch with standard yield; in mod_php5, the Apache library call rwrite is called.
  • sapi_error This is a handler for reporting mistakes to the application. Most SAPIs utilize php_error, which teaches PHP to utilize its own particular inside mistake framework.
  • flush This advises the application to flush its yield. In the CLI, this is actualized by means of the C library call fflush; mod_php5 utilizes the Apache library rflush.
  • send_header This sends a solitary determined header to the customer. A few servers, (for example, Apache) have worked in capacities for taking care of header transmission. Others, (for example, the PHP CGI) oblige you to physically send them. Others still, (for example, the CLI) don’t handle sending headers by any stretch of the imagination.
  • send_headers This sends all headers to the customer.
  • read_cookies During SAPI enactment, if a read_cookies handler is characterized, it will be called to populateSG(request_info).cookie_data. This is then used to populate the $_COOKIE autoglobal.
  • read_post During SAPI enactment, if the demand technique is a POST (or if the php.ini variablealways_populate_raw_post_data is valid), the read_post handler is called to populate $HTTP_RAW_POST_DATA and$_POST.

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666


Please fill the FORM and we shall contact you soon.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s