Click here to Skip to main content
11,494,936 members (58,049 online)
Click here to Skip to main content

PHP4Apps: PHP Scripting from your application

, 20 Dec 2005 63.7K 733 29
Rate this:
Please Sign up or sign in to vote.
How to call the PHP engine from your application and receive the reply and any variables.

PHP Demo application

Introduction

Usually, running a PHP script in .NET involves shelling to the OS and piping the response back. With Serhiy Perevoznyk's PHP4Delphi, Delphi developers were able to utilize the Zend (PHP) engine without having to resort to the shell. Serhiy created an unmanaged DLL you can call from .NET to reap the benefits of using a scripting engine. If you are a Delphi developer, the full source of the wrapper is available from SourceForge under the PHP4Delphi project.

Background

If you are not familiar with PHP, it is a very popular web scripting language, similar to Perl and Python. Like those languages also, it has applications for the desktop as well as for the web.

This wrapper builds on the PHP engine, providing an interface to the php4ts.dll (for PHP version 4) or php5ts.dll. You will need this file in order to use the php4apps.dll. I included this file in the demo download, but the freshest version is available from the PHP project site.

Using the code

There are seven methods exposed in the wrapper DLL:

  • InitRequest - Initializes the engine.
  • ExecutePHP - Calls the engine with a file to run.
  • ExecuteCode - Calls the engine with a string to run.
  • GetResultText - Retrieves the result of the script from the engine.
  • RegisterVariable - Registers a variable with the engine, allowing you to retrieve the value later.
  • GetVariable - Retrieves the value of a registered variable after the engine has completed.
  • DoneRequest - Finalizes the engine.

A simple method to execute a file looks like this:

// Initialize the Request
int RequestID = InitRequest();

// Execute the engine
ExecutePHP(RequestID, openFileDialog1.FileName);

// Create a buffer for the reply
StringBuilder builder = new  StringBuilder();

// Call it once to get the length
builder.Capacity = GetResultText(RequestID, builder, 0);

// Call it a second time to get the value
GetResultText(RequestID, builder, builder.Capacity + 1);

// Finalize the engine
DoneRequest(RequestID);

Similar to this, evaluating a string would be just a replacement of the ExecutePHP method for the ExecuteCode method:

...
string code = "phpInfo();";
ExecuteCode(RequestID, code);
...

Notice that you did not need the <? ?> around the PHP code. You could include it if you wish.

Now, the interesting part comes when you can inject your own variables and retrieve the results. If the PHP code looks something like this:

$i="Hello ".$i;
print $i;

and if you want to change the value of $i without having to append your values to the script, you can do it with RegisterVariable to set the variable before execution:

string code = "$i=\"Hello \".$i; print $i;";

// Initialize the Request
int RequestID = InitRequest();

// Execute the engine
ExecuteCode(RequestID, code);

// Register a variable with the engine
RegisterVariable(RequestID, "i", "World!");

Now the result will be the requisite "Hello World!" To retrieve the value of $i from the engine, you only need to call GetVariable.

// Create a buffer for the variable reply
StringBuilder resultVariable = new StringBuilder();

// Call it once to get the length
resultVariable.Capacity = GetVariable(RequestID, "i", resultVariable, 0);

// Call it a second time to actually get the value
GetVariable(RequestID, "i", resultVariable, resultVariable.Capacity+1);

History

  • 1.0 - Initial release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Daaron
Web Developer
United States United States
Raised by wolves in the mean streets of Macintosh C/C++ for IBM and Motorola, moved on to Delphi and now C#. Ah, the mother's milk of Microsoft...

Comments and Discussions

 
GeneralPHP, Excel & MSSQL 2000 Pin
Shazz Rock16-Feb-09 1:22
memberShazz Rock16-Feb-09 1:22 
QuestionArrays Pin
LordPadriac16-Mar-07 5:46
memberLordPadriac16-Mar-07 5:46 
AnswerRe: Arrays Pin
Daaron17-Mar-07 16:33
memberDaaron17-Mar-07 16:33 
GeneralRe: Arrays Pin
gabereiser20-Apr-07 6:19
membergabereiser20-Apr-07 6:19 
GeneralPassing parameters to the PHP script Pin
mitDAS200114-Mar-07 1:01
membermitDAS200114-Mar-07 1:01 
GeneralRe: Passing parameters to the PHP script Pin
Daaron14-Mar-07 4:55
memberDaaron14-Mar-07 4:55 
QuestionRe: Passing parameters to the PHP script Pin
mitDAS200114-Mar-07 22:32
membermitDAS200114-Mar-07 22:32 
AnswerRe: Passing parameters to the PHP script Pin
gabereiser20-Apr-07 7:07
membergabereiser20-Apr-07 7:07 
QuestionAdding an variable from to Adressline Pin
DreaDcYp8-Mar-06 10:45
memberDreaDcYp8-Mar-06 10:45 
AnswerRe: Adding an variable from to Adressline Pin
Daaron8-Mar-06 11:11
memberDaaron8-Mar-06 11:11 
GeneralRe: Adding an variable from to Adressline Pin
MaDcYp9-Mar-06 11:22
memberMaDcYp9-Mar-06 11:22 
GeneralRe: Adding an variable from to Adressline Pin
Daaron9-Mar-06 12:10
memberDaaron9-Mar-06 12:10 
GeneralRe: Adding an variable from to Adressline Pin
DreaDcYp9-Mar-06 23:40
memberDreaDcYp9-Mar-06 23:40 
AnswerRe: Adding an variable from to Adressline Pin
kekergood30-Apr-07 6:13
memberkekergood30-Apr-07 6:13 
Generalrunning php script from c# web application Pin
sanju sharma19-Jan-06 22:25
membersanju sharma19-Jan-06 22:25 
GeneralRe: running php script from c# web application Pin
Daaron20-Jan-06 4:30
memberDaaron20-Jan-06 4:30 
GeneralRe: running php script from c# web application Pin
sanju sharma20-Jan-06 4:34
membersanju sharma20-Jan-06 4:34 
GeneralRe: running php script from c# web application Pin
Daaron20-Jan-06 4:35
memberDaaron20-Jan-06 4:35 
GeneralRe: running php script from c# web application Pin
sanju sharma23-Jan-06 0:22
membersanju sharma23-Jan-06 0:22 
GeneralPHP version Pin
Ashley van Gerven27-Dec-05 1:30
memberAshley van Gerven27-Dec-05 1:30 
GeneralRe: PHP version Pin
Daaron27-Dec-05 8:04
memberDaaron27-Dec-05 8:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 20 Dec 2005
Article Copyright 2005 by Daaron
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid