php 获取请求,PHP 扩展 - 获取请求信息

在 main/SAPI.h 下定义了会使用到的 HTTP Request 属性,在扩展中可以引用该头文件并使用 SG 宏来使用 SAPI 的全局变量,结构如下:

typedef struct _sapi_globals_struct {

void *server_context;

sapi_request_info request_info; // 请求信息

sapi_headers_struct sapi_headers;

int64_t read_post_bytes;

unsigned char post_read;

unsigned char headers_sent;

zend_stat_t global_stat;

char *default_mimetype;

char *default_charset;

HashTable *rfc1867_uploaded_files;

zend_long post_max_size;

int options;

zend_bool sapi_started;

double global_request_time;

HashTable known_post_content_types;

zval callback_func;

zend_fcall_info_cache fci_cache;

} sapi_globals_struct;

结构体 sapi_request_info 结构如下:

typedef struct {

const char *request_method; // 请求方法,如 GET / POST / PUT 这些

char *query_string; // Query 字符串,就是 url ? 问号后面的 GET 参数

char *cookie_data; // Cookie 串

zend_long content_length; // 请求长度

char *path_translated;

char *request_uri; // 请求 URI

struct _php_stream *request_body;

const char *content_type; // 内容类型

zend_bool headers_only;

zend_bool no_headers;

zend_bool headers_read;

sapi_post_entry *post_entry; // Post 内容

char *content_type_dup;

/* for HTTP authentication */ // 下面三个用于 HTTP 验证

char *auth_user;

char *auth_password;

char *auth_digest;

/* this is necessary for the CGI SAPI module */

char *argv0;

char *current_user;

int current_user_length;

/* this is necessary for CLI module */

int argc;

char **argv;

int proto_num;

} sapi_request_info;

比如我现在要获取是否为 POST 请求,那么

/* {{{ proto bool isPost(void) */

PHP_FUNCTION(isPost)

{

if (!SG(request_info).request_method) {

RETURN_FALSE;

}

RETURN_BOOL(0 == strcasecmp(SG(request_info).request_method, "POST"));

}

/* }}} */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值