Click here to Skip to main content
6,632,253 members and growing! (20,074 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

PHP4Apps: PHP Scripting from your application

By Daaron

How to call the PHP engine from your application and receive the reply and any variables.
C#.NET 1.1, Win2K, WinXP, Win2003, Vista, PHP, VS.NET2003, Dev
Posted:20 Dec 2005
Views:44,359
Bookmarked:24 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 4.00 Rating: 4.19 out of 5
1 vote, 11.1%
1

2
1 vote, 11.1%
3
2 votes, 22.2%
4
5 votes, 55.6%
5

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

About the Author

Daaron


Member
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...
Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralPHP, Excel & MSSQL 2000 PinmemberShazz Rock1:22 16 Feb '09  
QuestionArrays PinmemberLordPadriac5:46 16 Mar '07  
AnswerRe: Arrays PinmemberDaaron16:33 17 Mar '07  
GeneralRe: Arrays Pinmembergabereiser6:19 20 Apr '07  
GeneralPassing parameters to the PHP script PinmembermitDAS20011:01 14 Mar '07  
GeneralRe: Passing parameters to the PHP script PinmemberDaaron4:55 14 Mar '07  
QuestionRe: Passing parameters to the PHP script PinmembermitDAS200122:32 14 Mar '07  
AnswerRe: Passing parameters to the PHP script Pinmembergabereiser7:07 20 Apr '07  
QuestionAdding an variable from to Adressline PinmemberDreaDcYp10:45 8 Mar '06  
AnswerRe: Adding an variable from to Adressline PinmemberDaaron11:11 8 Mar '06  
GeneralRe: Adding an variable from to Adressline PinmemberMaDcYp11:22 9 Mar '06  
GeneralRe: Adding an variable from to Adressline PinmemberDaaron12:10 9 Mar '06  
GeneralRe: Adding an variable from to Adressline PinmemberDreaDcYp23:40 9 Mar '06  
AnswerRe: Adding an variable from to Adressline Pinmemberkekergood6:13 30 Apr '07  
Generalrunning php script from c# web application Pinmembersanju sharma22:25 19 Jan '06  
GeneralRe: running php script from c# web application PinmemberDaaron4:30 20 Jan '06  
GeneralRe: running php script from c# web application Pinmembersanju sharma4:34 20 Jan '06  
GeneralRe: running php script from c# web application PinmemberDaaron4:35 20 Jan '06  
GeneralRe: running php script from c# web application Pinmembersanju sharma0:22 23 Jan '06  
GeneralPHP version PinmemberAshley van Gerven1:30 27 Dec '05  
GeneralRe: PHP version PinmemberDaaron8:04 27 Dec '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Dec 2005
Editor: Smitha Vijayan
Copyright 2005 by Daaron
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project