MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP Streams

Registering a stream wrapper


A stream wrapper provides a handler for one or more specific schemes.

The example below shows a simple stream wrapper that sends PATCH HTTP requests when the stream is closed.

// register the FooWrapper class as a wrapper for foo:// URLs.
stream_wrapper_register("foo", FooWrapper::class, STREAM_IS_URL) or die("Duplicate stream wrapper
registered");
class FooWrapper {
 // this will be modified by PHP to show the context passed in the current call.
 public $context;
 // this is used in this example internally to store the URL
 private $url;
 // when fopen() with a protocol for this wrapper is called, this method can be implemented to
store data like the host.
 public function stream_open(string $path, string $mode, int $options, string &$openedPath) :
bool {
 $url = parse_url($path);
 if($url === false) return false;
 $this->url = $url["host"] . "/" . $url["path"];
 return true;
 }
 // handles calls to fwrite() on this stream
 public function stream_write(string $data) : int {
 $this->buffer .= $data;
 return strlen($data);
 }
 // handles calls to fclose() on this stream
 public function stream_close() {
 $curl = curl_init("http://" . $this->url);
 curl_setopt($curl, CURLOPT_POSTFIELDS, $this->buffer);
 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
 curl_exec($curl);
 curl_close($curl);
 $this->buffer = "";
 }
 // fallback exception handler if an unsupported operation is attempted.
 // this is not necessary.
 public function __call($name, $args) {
 throw new \RuntimeException("This wrapper does not support $name");
 }
 // this is called when unlink("foo://something-else") is called.
 public function unlink(string $path) {
 $url = parse_url($path);
 $curl = curl_init("http://" . $url["host"] . "/" . $url["path"]);
 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
 curl_exec($curl);
 curl_close($curl);
 }
}

This example only shows some examples of what a generic stream wrapper would contain. These are not all methods available. A full list of methods that can be implemented can be found at http://php.net/streamWrapper.

Conclusion

In this page (written and validated by ) you learned about PHP Streams . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Type hinting.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.