Interesting streams in PHP

Source: Internet
Author: User
Tags http request numeric require resource wrapper

Interesting stream

One feature that PHP often mentions is the streaming context. This optional parameter is even available in most user-space-related functions, as a generalized framework for implementing incoming/outgoing additional information to a given wrapper or stream.

Context

The context of each stream contains two types of internal messages. The first and most common is the contextual selections. These values are arranged in a two-dimensional array in the context, and are typically used to change the initialization behavior of the flow wrapper. There is also a context parameter that is unknown to the wrapper and currently provides a way to notify the event within the flow wrapper layer.

Php_stream_context *php_stream_context_alloc (void);

This API call allows you to create a context that allocates some storage space and initializes the Hashtable for saving context options and parameters. It is also automatically registered as a resource that will be cleaned after a request terminates.

Setting options

The internal API for setting context options and the API for user space are equivalent:

int php_stream_context_set_option (Php_stream_context *context,  
            const char *wrappername, const char *optionname,  
            zval *optionvalue);

The following is a prototype of user space:

BOOL Stream_context_set_option (Resource $context,  
            string $wrapper, String $optionname,  
            mixed $value);

The difference is only between user space and the internal needs of the data types. The following example uses these two API calls to initiate an HTTP request through the built-in wrapper and overwrite the User_agent setting with a contextual selection.

Php_stream  *php_varstream_get_homepage (const char *alt_user_agent tsrmls_dc)  
{  
    Php_stream_context  *context;  
    Zval    Tmpval;  
      
    Context = Php_stream_context_alloc (Tsrmls_c);  
    Zval_string (&tmpval, alt_user_agent, 0);   
    Php_stream_context_set_option (Context, "http", "User_agent", &tmpval);  
    Return php_stream_open_wrapper_ex ("Http://www.php.net", "RB", Report_errors | Enforce_safe_mode, NULL, context);  

The translator uses the php-5.4.10 Php_stream_context_alloc () to increase the thread safety control, so the corresponding example is modified, please note when the reader tests.

The note here is that Tmpval does not allocate any persistent storage space, and its string values are set by replication. Php_stream_context_set_option () automatically makes a copy of the incoming Zval content.

Retrieve options

The API call for retrieving context options is exactly the mirror of the corresponding settings API:

int php_stream_context_get_option (Php_stream_context *context,  
            const char *wrappername, const char *optionname,  
            zval ***optionvalue);

In retrospect, the contextual items are stored in a nested hashtable, and when a value is retrieved from a Hashtable, the general method is to pass a pointer to the zval * * to Zend_hash_find (). Of course, because Php_stream_context_get_option () is a special agent of Zend_hash_find (), their semantics are the same.

The following is an example of a simplified version of the built-in HTTP wrapper using the php_stream_context_get_option () setting User_agent:

Zval **ua_zval;  
Char *user_agent = "php/5.1.0";  
If (Context &&  
    php_stream_context_get_option (context, "http",  
                "User_agent", &ua_zval) = = SUCCESS &&  
                z_type_pp (ua_zval) = = is_string) {  
    user_agent = z_strval_pp (Ua_zval);  
}

In this case, the non string value will be discarded because the value is meaningless for the user agent string. Other contextual options, such as max_redirects, require numeric values, and because storing numeric values in the Zval of a string is not universal, you need to perform a type conversion to make the settings legal.

Unfortunately, these variables are context-owned, so they cannot be directly converted; You need to isolate and then convert, and then destroy if you need to:

Long max_redirects =;  
Zval **tmpzval;  
If (Context &&  
    php_stream_context_get_option (context, "http",  
            "Max_redirects", &tmpzval) = = SUCCESS) {  
    if (z_type_pp (tmpzval) = = Is_long) {  
        max_redirects = z_lval_pp (Tmpzval);  
    } else {  
        zval Copyval = **tmpzval;  
        Zval_copy_ctor (? val);  
        Convert_to_long (? val);  
        Max_redirects = Z_lval (copyval);  
        Zval_dtor (? val);  
    }  
}

In fact, in this case, Zval_dtor () is not necessary. Is_long variables do not require storage space outside the Zval container, so zval_dtor () actually does not have a real operation. In this case it is included for completeness, and for strings, arrays, objects, resources, and other possible types of the future, this call is required.

Parameters

Although the user space API looks like the parameters and contextual items are similar, they are actually defined as different members in the PHP_STREAM_CONTEXT structure within the language.

Only one context parameter is currently supported: the notification device. This element in the PHP_STREAM_CONTEXT structure can point to the following php_stream_notifier structure:

typedef struct {  
    php_stream_notification_func func;  
    void (*dtor) (Php_stream_notifier *notifier);  
    void *ptr;  
    int mask;  
    size_t progress, Progress_max;  
Php_stream_notifier;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.