Transferring data to memory from PHP to a .Net program

How to transfer data in memory from PHP to a .Net program? I will use Process to call php.exe and pass the script name (* .php) and arguments.

Now the problem is how to transfer data from PHP to .Net?

In particular, I am looking at how PHP can pass data so that .Net can intercept it. The .Net code I have is similar to this:

Process p = new Process();
StreamWriter sw;
StreamReader sr;
StreamReader err;
ProcessStartInfo psI = new ProcessStartInfo("cmd");
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.RedirectStandardError = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;
p.Start();
sw = p.StandardInput;
sr = p.StandardOutput;
var text1 = sr.ReadToEnd();  // the php output should be able to be read by this statement
sw.Close();

Edit: some suggest using XML, which is good. But XML is a file system; I would prefer that data interaction be passed in memory to prevent accidental writing to the same XML file.

+3
source share
4 answers

Inspire solution, :

PHP:

<?php

$stdout = fopen('php://stdout', 'w');
$writeString ="hello\nme\n";
fwrite($stdout, $writeString);
fclose($stdout);

.Net:

[Test]
public void RunConsole()
{
    Process p = new Process();

    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = @"C:\Program Files\PHP\php.exe";
    p.StartInfo.Arguments = "\"C:\\Documents and Settings\\test\\My Documents\\OurPHPDirectory\\OutputData.php\"";
    p.Start();
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    Assert.AreEqual(0, p.ExitCode);
    Assert.AreEqual("hello\nme\n", output);

}
-2

PHP STDOUT:

<?php
$stdout = fopen('php://stdout', 'w');

.NET . .NET, :

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

+3

XML. , , , .

, , .

+1

PHP, , -, - .net, php-

0

Source: https://habr.com/ru/post/1718531/


All Articles