Why do I need a protocol wrapper to parse a PHP file?
There are some cases in which one wants to pass a dynamic string to a function that only takes a file path. One of those cases is the SoapServer class in PHP. The SoapServer class takes the $WSDL argument as a local file path. If you ever needed to have a dynamic WSDL file for different customers you would probably know what I’m talking about.
Creating the Protocol/Wrapper to parse a file using PHP
The trick here is to use the PHP function stream_wrapper_register to define a protocol that parses PHP.
1. create a class handler for the new protocol wrapper. Click on the link to see the code.
Note that I have added functionality to accept a query string.
2. Create your dynamic file
hello.php
Hello <?=$this->first_name?>!
3. Register your Protocol/Wrapper some where in your code and use it!
<?php
//include the class
include('PhpFileStream.php');
//
//register the wrapper
stream_wrapper_register('phpfile', 'PhpFileStream');
//
//you can now pass the file path with the new protocol wrapper and it will be parsed
echo file_get_contents('phpfile:///./hello.php?first_name=John');
This code will print:
Hello John!
Note that You can pass a URL encoded query string to the file. The values can be accessed from within the file using $this->[key_name] in the target file.
Now all you would have to do If you ever needed to create a dynamic WSDL file was to use the class I provide here and pass the wrapper path this way:
<?php
//...
SoapServer('phpfile:///path/to/dynamic.wsdl?key1=val1&key2=val2');
//...
Now you should be able to include PHP code in you WSDL for you dynamic needs
Enjoy
Tags: Dynamic, PHP, Programming, SOAP, WSDL