简体   繁体   中英

stream_context_set_params vs. stream_context_set_option

What The Documentation Says

From reading php.net it seems to me that stream_context_set_params pretty much does the same thing as stream_context_set_option. ie.

http://www.php.net/manual/en/function.stream-context-set-params.php

bool stream_context_set_params ( resource $stream_or_context , array $params )

http://www.php.net/manual/en/function.stream-context-set-option.php

bool stream_context_set_option ( resource $stream_or_context , array $options )

stream_context_set_option supports additional parameters that stream_context_set_params doesn't but otherwise it seems like they're doing the same thing. At least in theory.

What My Tests Show

My own testing would suggest otherwise and actually leaves me wondering what stream_context_set_params actually does (if anything).

Using stream_context_set_params ...

<?php
$ctx = stream_context_create();
stream_context_set_params($ctx, array('zz' => array('zz' => 'zz')));
print_r(stream_context_get_options($ctx));

That prints out the following (which surprises me):

Array
(
)

Using stream_context_set_option ...

<?php
$ctx = stream_context_create();
stream_context_set_option($ctx, array('zz' => array('zz' => 'zz')));
print_r(stream_context_get_options($ctx));

That prints out the following (as I'd expect):

Array
(
    [zz] => Array
        (
            [zz] => zz
        )

)

So I really have no clue. Any ideas?

bool stream_context_set_params ( resource $stream_or_context , array $params )

on this time it's just take 'notification' param key, and used by stream_notification_callback .

you can see the list of Supported Context Parameters here: http://php.net/manual/en/context.params.php

<?php $opts = array(
        'http'=>array(
            'method'=>"GET",
            'header'=>"Accept-language: en\r\n" .
                "Cookie: foo=bar\r\n"
        ),
    );

    $context = stream_context_create($opts);
    stream_context_set_params($context
        , ['notification' => 'your_call_back_notification']
    );


    var_dump(stream_context_get_params($context));

Output:

Array(
[notification] => your_call_back_notification
[options] => Array
    (
        [http] => Array
            (
                [method] => GET
                [header] => Accept-language: en
                            Cookie: foo=bar

            )

    )
)

You may have an warning error for notification call back, it must be a valid callable. look at http://php.net/manual/en/function.stream-notification-callback.php for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM