JavaScript stream_context_create
Create a file context and optionally set parameters
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 | function stream_context_create (options, params) { // Create a file context and optionally set parameters // // version: 1109.2015 // discuss at: http://phpjs.org/functions/stream_context_create // + original by: Brett Zamir (http://brett-zamir.me) // % note 1: Can be made to work as a wrapper for proprietary contexts as well // * 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: get_resource_type(context); // * returns 1: 'stream-context' var resource = {}; options = options || {}; // params.notification is only property currently in PHP for params // BEGIN REDUNDANT this.php_js = this.php_js || {}; this.php_js.resourceIdCounter = this.php_js.resourceIdCounter || 0; function PHPJS_Resource(type, id, opener) { // Can reuse the following for other resources, just changing the instantiation // See http://php.net/manual/en/resource.php for types this.type = type; this.id = id; this.opener = opener; } PHPJS_Resource.prototype.toString = function () { return 'Resource id #' + this.id; }; PHPJS_Resource.prototype.get_resource_type = function () { return this.type; }; PHPJS_Resource.prototype.var_dump = function () { return 'resource(' + this.id + ') of type (' + this.type + ')'; // return 'resource('+this.id+'), '+this.type+')'; another format }; // END REDUNDANT this.php_js.resourceIdCounter++; resource = new PHPJS_Resource('stream-context', this.php_js.resourceIdCounter, 'stream_context_create'); resource.stream_options = options; resource.stream_params = params; return resource; } |
Examples
Running
1 2 3 | var opts = {http:{ method:'GET', header: 'Accept-language: en\r\nCookie: foo=bar\r\n' } }; var context = stream_context_create(opts); get_resource_type(context); |
Should return
1 | 'stream-context' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have stream_context_create goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>