dongqun9403 2010-05-21 08:33
浏览 20
已采纳

PHP类到C#类?

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 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.

  • 写回答

6条回答 默认 最新

  • doutuohan6606 2010-05-21 08:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog