<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px">1、首先概念普及:</span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px"><span style="color:rgb(34,34,34); font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; text-indent:12px">SAPI: Server abstraction API,它提供了一个接口,使得PHP可以和其他应用进行交互数据,具体点说是<span style="color:rgb(34,34,34); font-family:"Helvetica Neue",Helvetica,Arial,sans-serif">提供了一个和外部通信的接口。常见的:给apache的mod_php5,CGI,给IIS的ISAPI,还有Shell的CLI</span></span></span></p>
<p><span style="font-size:14px; color:rgb(34,34,34); font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; text-indent:12px">首先我们看个从鸟哥那挪来的PHP架构图:</span></p>
<p style="text-align:center"><span style="font-size:14px; color:rgb(34,34,34); font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; text-indent:12px"><img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-93c4a5edd5713132d233acba50e21645"><br> </span></p>
<p><span style="font-family:Arial; color:#333333"><span style="font-size:14px">如果还感觉概念模糊的话 可以试着用wamp升级php版本来找下感觉</span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px">首先说下本篇以Apache sapi 为例来介绍,对sapi的功能实现比较全嘛, 还<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px">常用</span>。</span></p>
<p><span style="font-family:Arial; color:#333333"><span style="font-size:14px">如果想看简单的可从cgi入手,鸟哥有对此介绍:http://www.laruence.com/2008/08/12/180.html</span></span></p>
<p><span style="font-family:Arial; color:#333333"><span style="font-size:14px"><br> </span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px">2、接下来我们来看具体的实现:</span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px">要定义个SAPI,首先要定义个sapi_module_struct,</span></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px">查看源码:php-5.5.12/sapi/apache2handler/sapi_apache2.c:</span></p>
<p></p>
<pre class="blockcode"><code class="language-cpp">static sapi_module_struct apache2_sapi_module = {
"apache2handler", /* 输出给php_info()使用 */
"Apache 2.0 Handler", /* pretty name */
php_apache2_startup, /* startup */ /* 当SAPI初始化时,首先会调用该函数 */
php_module_shutdown_wrapper, /* shutdown */ /* 关闭函数包装器,它用来释放所有的SAPI的数据结构、内存等,调用php_module_shutdown */
NULL, /* activate */ /* 此函数会在每个请求开始时调用,它会做初始化,资源分配 */
NULL, /* deactivate */ /* 此函数会在每个请求结束时调用,它用来确保所有的数据都得到释放 */
php_apache_sapi_ub_write, /* unbuffered write */ /* 不缓存的写操作(unbuffered write),它是用来向SAPI外部输出数据 */
php_apache_sapi_flush, /* flush */ /* 刷新输出,在CLI模式下通过使用C语言的库函数fflush实现 */
php_apache_sapi_get_stat, /* get uid */ /* */
php_apache_sapi_getenv, /* getenv */ /* 根据name查找环境变量 */
php_error, /* error handler */ /* 注册错误处理函数 */
php_apache_sapi_header_handler, /* header handler */ /* PHP调用header()时候被调用 */
php_apache_sapi_send_headers, /* send headers handler *//* 发送头部信息 */
NULL, /* send header handler */ /* 发送一个单独的头部信息 */
php_apache_sapi_read_post, /* read POST data */ /* 当请求的方法是POST时,程序获取POST数据,写入$_POST数组 */
php_apache_sapi_read_cookies, /* read Cookies */ /* 获取Cookie值 */
php_apache_sapi_register_variables, /* register server variables 给$_SERVER添加环境变量 */
php_apache_sapi_log_message, /* Log message */ /* 输出错误信息 */
php_apache_sapi_get_request_time, /* Request Time */ /* */
NULL, /* Child Terminate */ /* */
STANDARD_SAPI_MODULE_PROPERTIES
};</code></pre>
<br>
<p></p>
<p><span style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px">由上面代码,再结合<span style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px">SAPI.h和SAPI.c,其实可以看出 </span>PHP的SAPI</span><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; background-color:rgb(255,255,255)"><span style="color:#3366ff">像是 </span></span><span style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px">面向对象中基类,SAPI.h和SAPI.c包含的函数是抽象基类的</span><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px"><span style="color:#3366ff">声明和定义</span></span><span style="color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,s |
|