简体   繁体   English

stream_context_set_params与stream_context_set_option

[英]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. 从阅读php.net,我觉得stream_context_set_params几乎和stream_context_set_option一样。 ie. 即。

http://www.php.net/manual/en/function.stream-context-set-params.php 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 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. stream_context_set_option支持stream_context_set_params没有的其他参数,否则看起来他们正在做同样的事情。 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). 我自己的测试建议不然,实际上让我想知道stream_context_set_params实际上做了什么(如果有的话)。

Using stream_context_set_params ... 使用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 ... 使用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 . 在这个时候它只是采用'notification'参数密钥,并由stream_notification_callback

you can see the list of Supported Context Parameters here: http://php.net/manual/en/context.params.php 您可以在此处查看支持的上下文参数列表: 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. 请访问http://php.net/manual/en/function.stream-notification-callback.php了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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