Usually, running a PHP script in .NET involves shelling to the OS and piping the response back. With Serge Perevoznyk's PHP4Delphi, Delphi developers were able to utilize the Zend (PHP) engine without having to resort to the shell while accruing some superb features, like User-Defined Functions, Variables, Constants, and Error events. PHPX is an ActiveX control to wrap this functionality. I needed it for a project and I thought I'd share it with all of you.
- PHPXControl.ocx - an ActiveX wrapper to the PHP4Delphi library. This file must be registered in your OS using regsvr32 (at the command line, type "regsvr32 phpxcontrol.ocx").
- php4Delphi.dll - Serge Perevoznyk's library to interpret the getting and setting of information with the Zend engine (included in ZIP).
- php4ts.dll or php5ts.dll - The PHP Zend engine, not included in this ZIP. This file and the entire PHP engine package is freely available from the PHP Site, but all you need is php*ts.dll.
(Of course, the ActiveX control can be used in C++, C#, and VB/VB.NET also.)
var php = new ActiveXObject("PHPXControl.PHPX");
php.DLLFolder = "c:\\phpx\\";
var result = php.RunCode('print "Hello World!";');
Using The Control
After instantiating the control, set the DLLFolder property. If this is not set, you'll receive an error that, the script engine could not start. As a general rule, I suggest you have the components listed above all in the same directory for ease of use. Creating the control more than once will expose a bug in the underlying Delphi code, so I suggest against it.
Running a script is done using a
Execute method. The difference is that,
RunCode wants your code as a string where
Execute wants a filename.
The coolest feature (for me) is the ability to add functions, variables, and constants to PHP. You read it right-- add functions to PHP. You can register your code function using the
AddFunction method. You receive an
OnFunctionExecute event when your method is fired from within PHP, and you can return information to PHP using the
ReturnValue reference variable.
At the end of your application or script, you also need to call the
FinalizeControl method to free the memory in the underlying code.
(Read as "using this for good and not evil")
Use at your own risk.
This is very useful and at the same time, potentially harmful functionality.
Some points about browsers and this control:
- The ActiveX control only works in browsers that support them, namely the Internet Explorer.
- Even in IE, you must specifically allow ActiveX controls.
- I implore you to never freely enable running ActiveX controls. Instead, I suggest allowing them only on a trusted site, and then only at a prompt setting.
I hope you enjoy the code, and please pardon some of the messy extra properties in the control. I admit, I was a little lazy here. Delphi requires that you select a visual control in order to quickly make an ActiveX control, so you get some of the baggage along with it. If I have a few requests, I'll re-create the project without the extras. I will maintain a link for the latest version at angorasoftware.com