tags:

views:

99

answers:

4

Hello, I browsed internet but haven't found my answer... as I'm running out of ideas, I'd appreciate some help on this issue. So here it is: I have some PHP code that takes a path to a file and returns a bunch of information that i need. I have the source code (it is open source), and i know my C#. My problem is i want to be able to use this PHP code from a C# application, offline, emulating the PHP code.

Any ideas?

Thanks.

A: 

It sounds like to either wish to

  1. Translate the PHP code to C#
  2. Call the PHP code from .NET
  3. ???

I don't think emulation is the term you're looking for, exactly.

If you can link to the PHP code (if it's OSS, is there a site with more information?) or paste it here, we can probably let you know the best way to handle this.

Alex JL
i want to call the PHP code from .NET C#
FatalBaboon
http://kuukkeli.ath.cx/ooaemkee/SC2Replay/upload_file.php
FatalBaboon
the PHP page im trying to imitate is the one above
FatalBaboon
We can't view the PHP from the link, for obvious reasons...
siride
weird, i can click the link from my side oO
FatalBaboon
Sorry, I missed the link on the page to the source code. Ignore my comment :-/
siride
@FatalBaboon so.. this is a bit more complex than it sounded like. Translating it to C# might take a good bit of effort (and there's C in there, too?). Probably Tim's tips are going to be the most helpful.
Alex JL
+1  A: 

If you really must, you can always launch the PHP interpreter from C# by spawning a process. I'll leave the details to MSDN. Better, though, would be to just translate the PHP to C# by hand.

siride
Translating PHP to C# by hand is my last resort, i wanted to ask for a more straight forward way to handle this issue before i'd do it...
FatalBaboon
+1  A: 

You could set up a PHP server on the local machine and invoke the file using an HTTP request created with c# (such as with the WebRequest object). This satisfies the requirement of working offline and since you have the PHP code, you can make minor modifications to accommodate whatever parameters you need to pass into it.

However unless this is for an isolated case in a controlled environment, I would not suggest it.

If this is for distribution to user machines it would be a) impractical to deploy and b) a maintenance nightmare. If you are running on a server and rewriting the PHP in c# is not an option, it should get the job done.

Tim
Yeah it is for distribution, on final stage =X
FatalBaboon
There may be some possibilities with the DLR (Dynamic Language Runtime) in .Net 4.0. This allows stronger typed languages like c# to interface with interpreted languages like PHP. I can't find a specific example but this could be a good place for you to start.
Tim
A: 

If you don't want to translate the PHP code into C# then you must invoke the PHP interpreter.

First, look at the license on the PHP code you would be using. When I looked at the code earlier I noticed that all but one PHP file had a GNU GPL license on it. GNU GPL should work for what you want, but I recommend that you review the GNU GPL if you haven't done so already. You should also look into what license is supposed to be applied to the last file before continuing.

Second, rewrite the entry file. The file in question is upload_file.php. This file loads the other files as includes, parses the SC2 replay file, and generates an output for use by a web browser. You just need to rewrite this file to have it output a simplified version of its output to the console. You might have it receive the name of the replay file from command line arguments. The other files that are marked as GNU GPL can just be copied directly into your project. I should also say here that there may already be a console version. Take a minute to look around for one.

Third, write a C# application. Invoke the PHP interpreter telling it the what file to interpret, and the name of the replay file to parse. You will need to redirect the console output from your PHP instance into your C# code to be able to read the output.

Fourth, read the output.

Fifth, parse the output for use in your C# application.

Lunatic Experimentalist
Thank you for your answer1) I know about the license, it is fine by me.2)I came to the same conclusion about the file to rewrite, but I have no knowledge at all in PHP =X I didn't even know PHP could write into the console.I took a long look around before chosing this PHP open source project as my best option.As far as i understood, i need to rewrite the first 63 lines, that contains javascript (no idea what that part does tbh), and some HTML that generates a form. The only part in PHP is the one that says not to upload a file over 4Mo (if i got that right).
FatalBaboon
And last but not least, you talk about a "PHP interpreter", do you mean Phalanger? (they use/made phpc, as far as i know).Part 3/4/5 is no problem at all.
FatalBaboon
By PHP interpreter I mean the official PHP-CLI. The echo statement in the PHP-CLI writes to console output. I have heard that there were PHP compilers available, but haven't looked into them. I'm not sure exactly how using Phalanger would change what you have to do in this case.
Lunatic Experimentalist