美文网首页PHP实战
PHP 扩展 - 获取请求信息

PHP 扩展 - 获取请求信息

作者: Bun_Wong | 来源:发表于2016-07-09 22:25 被阅读285次

    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"));
    }
    /* }}} */
    
    

    相关文章

      网友评论

        本文标题:PHP 扩展 - 获取请求信息

        本文链接:https://www.haomeiwen.com/subject/jafdjttx.html