开发者

PHP class to C# class?

I work for a company that makes application's in C#. recently we got a customer asking us to look in to rebuilding an application written in PHP. This application receives GPS data from car mounted boxes and processes that into workable information.

The manufacturer for the GPS device has a PHP class that parses the received information and extracts coordinates. We were looking in to rewriting the PHP class to a C# class so we can use it and adapt it. And here it comes, on the manufacturers website there is a singel line of text that got my skin krawling:

"The encoding format and contents of the transmitted data are subject to constant changes. This is caused by implementations of additional features by new module firmware versions which makes it virtually impossible开发者_如何学Python to document it and for you to properly decode it yourself."

So i am now looking for a option to use the "constantly changing" PHP class and access it in C#. Some thing link a shell only exposing some function's i need. Except i have no idea how i can do this. Can any one help me find a solution for this.


I know it's a really hacky solution, but if you need a bit of PHP code that you don't want to have to repeatedly port to C# each time, you could try the following approach, although it means that you would need the php command line tool on the target machine.

First step is to have a php script that continously reads data off stdin, decodes it using this special class from the vendor, and writes the result out to stdout. Really simple example:

<?php

include("VendorDecodingClass.php");

while(true) 
{
    $input = fgets(STDIN); //read off of the stdin stream

    //can't remember if this is valid, but somehow check that there is some data
    if($input) 
    {
         //pass it off to the vendor decoding class
         $output = VendorDecoding::decode($input);    

         fwrite(STDOUT, $output); //write the results back out
    }
    //sleep here so you don't suck up CPU like crazy 
    //(1 second may be a bit long tho, may want usleep)
    //Edit: From Tom Haigh, fgets will block, so the sleep isn't necessary
    //sleep(1); 
}

?>

Anyway, once you have that in place, in your C# application, right at the start, create a new Process to run that script and then save the Process instance somewhere, so you can reference the STDIN and STDOUT at a later point. Example:

ProcessStartInfo procStartInfo = new ProcessStartInfo("php", "yourscript.php");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;

Process proc = new Process(); //store this variable somewhere
proc.StartInfo = procStartInfo;
proc.Start();

Then, when you want to decode your data, you just write to the stdin of the php process you created, and wait for a response on the stdout. Using the stdin/stdout approach is a lot more efficient than creating a new process each time you want to decode some data, because the overhead of creating that process can be noticeable.

proc.StandardInput.WriteLine(somedata); //somedata is whatever you want to decode

//may need to wait here, or perhaps catch an exception on the next line?

String result = proc.StandardOutput.ReadLine();

//now result should contain the result of the decoding process

Disclaimer here, I haven't tested any of this code, but that is the general gist of how I might do it.

Something else I just thought of, you will want some mechanism for terminating that PHP process. It may be OK to use Process.Kill, but if the decoding does any file IO, or anything critical you may want to send an interrupt signal to the php script somehow.


I assume the php script is on your machine and returns usefull data. The first -not very elegant solution- that pops into my mind is the following: Make sure your machine has the php commandline installed, so that you are able to run the php script from commandline. To execute a commandlinetool from C# see code for that here. The returned data now probably needs to get processed my your C# program.


I have never tried this and do not know anyone that has, but I remember comming across this sometime ago and thought I would throw it out there as a possible option for you.

Phalanger is a compiler project that compiles PHP code to IL, so you can use that then have a managed assembly that you reference from your code directly.


If the format is a regex you can try to put it in an application setting file (not resources, these are compiled WITH the application, you can't change them without recompiling the app).

Application settings are not changeable by the user but you can do that by editing the XML.

Or you can set the settings to user mode and then you can change the format from inside your application code.


Why don't you just launch the PHP script from C#, have it output its results to a file and then use that file as input for your C# program?


Personally, I would setup a PHP web service with a proper and stable API that the C# project can access, implement the manufacturers supplied PHP class in the web service and let it be.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜