Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8305681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:10:05+00:00 2026-06-08T18:10:05+00:00

I’m writing a custom stream wrapper to use as a stub in unit tests

  • 0

I’m writing a custom stream wrapper to use as a stub in unit tests for an HTTP client class that uses the built-in http:// stream wrapper.

Specifically, I need control over the value returned in the 'wrapper_data' key by calls to stream_get_meta_data on streams created by the custom stream wrapper. Unfortunately, the documentation on custom stream wrappers is woeful and the API seems unintuitive.

What method in a custom wrapper controls the the meta wrapper_data response?

Using the class at the bottom I’ve only been able to get the following result when I var_dump(stream_get_meta_data($stream)); on streams created with the custom wrapper …

array(10) {
  'wrapper_data' =>
  class CustomHttpStreamWrapper#5 (3) {
    public $context =>
    resource(13) of type (stream-context)
    public $position =>
    int(0)
    public $bodyData =>
    string(14) "test body data"
  }
  ...

But I need to coax the wrapper into yielding something like the following on meta data retrieval so I can test the client class’s parsing of the data returned by the real http:// stream wrapper …

array(10) {
  'wrapper_data' => Array(
       [0] => HTTP/1.1 200 OK
       [1] => Content-Length: 438
   )
   ...

Here’s the code I have currently for the custom wrapper:

class CustomHttpStreamWrapper {

    public $context;
    public $position = 0;
    public $bodyData = 'test body data';

    public function stream_open($path, $mode, $options, &$opened_path) {
        return true;
    }

    public function stream_read($count) {
        $this->position += strlen($this->bodyData);
        if ($this->position > strlen($this->bodyData)) {
            return false;
        }
        return $this->bodyData;
    }

    public function stream_eof() {
        return $this->position >= strlen($this->bodyData);
    }

    public function stream_stat() {
        return array('wrapper_data' => array('test'));
    }

    public function stream_tell() {
        return $this->position;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:10:06+00:00Added an answer on June 8, 2026 at 6:10 pm

    stream_get_meta_data is implemented in ext/standard/streamfunc.c. The relevant part is

    if (stream->wrapperdata) {
        MAKE_STD_ZVAL(newval);
        MAKE_COPY_ZVAL(&stream->wrapperdata, newval);
    
        add_assoc_zval(return_value, "wrapper_data", newval);
    }
    

    i.e. whatever zval stream->wrapperdata holds is “copied” to/referenced by $retval[“wrapper_data”].
    Your custom wrapper code is “handled” by user_wrapper_opener in main/streams/userspace.c. And there you have

    /* set wrapper data to be a reference to our object */
    stream->wrapperdata = us->object;
    

    us->object “is” the instance of your custom wrapper that has been instantiated for the stream. I haven’t found a way to influence stream->wrapperdata from userspace scripts other than that.
    But you could implement Iterator/IteratorAggregate and/or ArrayAccess if all you need is foreach($metadata['wrapper_data'] ...) and $metadata['wrapper_data'][$i].
    E.g.

    <?php
    function test() {
        stream_wrapper_register("mock", "CustomHttpStreamWrapper") or die("Failed to register protocol");
        $fp = fopen("mock://myvar", "r+");
        $md = stream_get_meta_data($fp);
    
        echo "Iterator / IteratorAggregate\n";
        foreach($md['wrapper_data'] as $e) {
            echo $e, "\n";
        }
    
        echo "\nArrayAccess\n";
        echo $md['wrapper_data'][0], "\n";
    
        echo "\nvar_dump\n";
        echo var_dump($md['wrapper_data']);
    }
    
    class CustomHttpStreamWrapper implements IteratorAggregate, ArrayAccess  {
        public $context;
        public $position = 0;
        public $bodyData = 'test body data';
    
        protected $foo = array('HTTP/1.1 200 OK', 'Content-Length: 438', 'foo: bar', 'ham: eggs');
        /* IteratorAggregate */
        public function getIterator() {
            return new ArrayIterator($this->foo);
        }
        /* ArrayAccess */
        public function offsetExists($offset) { return array_key_exists($offset, $this->foo); }
        public function offsetGet($offset ) { return $this->foo[$offset]; }
        public function offsetSet($offset, $value) { $this->foo[$offset] = $value; }
        public function offsetUnset($offset) { unset($this->foo[$offset]); }
    
        /* StreamWrapper */
        public function stream_open($path, $mode, $options, &$opened_path) {
            return true;
        }
    
        public function stream_read($count) {
            $this->position += strlen($this->bodyData);
            if ($this->position > strlen($this->bodyData)) {
                return false;
            }
            return $this->bodyData;
        }
    
        public function stream_eof() {
            return $this->position >= strlen($this->bodyData);
        }
    
        public function stream_stat() {
            return array('wrapper_data' => array('test'));
        }
    
        public function stream_tell() {
            return $this->position;
        }
    }
    
    test();
    

    prints

    Iterator / IteratorAggregate
    HTTP/1.1 200 OK
    Content-Length: 438
    foo: bar
    ham: eggs
    
    ArrayAccess
    HTTP/1.1 200 OK
    
    var_dump
    object(CustomHttpStreamWrapper)#1 (4) {
      ["context"]=>
      resource(5) of type (stream-context)
      ["position"]=>
      int(0)
      ["bodyData"]=>
      string(14) "test body data"
      ["foo":protected]=>
      array(4) {
        [0]=>
        string(15) "HTTP/1.1 200 OK"
        [1]=>
        string(19) "Content-Length: 438"
        [2]=>
        string(8) "foo: bar"
        [3]=>
        string(9) "ham: eggs"
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.