---
layout: page
title: "JavaScript stream_context_set_option function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/stream_context_set_option:849
- /functions/view/stream_context_set_option
- /functions/view/849
- /functions/stream_context_set_option:849
- /functions/849
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's stream_context_set_option

{% codeblock stream/stream_context_set_option.js lang:js https://raw.github.com/kvz/phpjs/master/functions/stream/stream_context_set_option.js raw on github %}
function stream_context_set_option (stream_or_context, optionsOrWrapper, option, value) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: var opts = {http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' } };
  // *     example 1: var context = stream_context_create(opts);
  // *     example 1: stream_context_set_option(context, opts);
  // *     returns 1: true
  if (option) {
    if (!stream_or_context.stream_options[optionsOrWrapper]) { // Don't overwrite all?
      stream_or_context.stream_options[optionsOrWrapper] = {};
    }
    stream_or_context.stream_options[optionsOrWrapper][option] = value;
  } else {
    stream_or_context.stream_options = optionsOrWrapper;
  }
  return true;
}
{% endcodeblock %}

 - [view on github](https://github.com/kvz/phpjs/blob/master/functions/stream/stream_context_set_option.js)
 - [edit on github](https://github.com/kvz/phpjs/edit/master/functions/stream/stream_context_set_option.js)

### Example 1
This code
{% codeblock lang:js example %}
var opts = {http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' } };
var context = stream_context_create(opts);
stream_context_set_option(context, opts);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}


### Other PHP functions in the stream extension
{% render_partial _includes/custom/stream.html %}
