3

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

The issue now is how do I pass back the data, from the PHP to the .Net?

Specifically, I am looking at the manner in which PHP can pass out the data so that .Net can intercept it. The .Net code I have are 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 the use of XML, which is fine. But XML is a file based system; I would prefer a manner in which the interaction of data are passed in memory, just to prevent accidentally writing to the same XML file.

1
  • 3
    "Some suggest the use of XML, which is fine. But XML is a file based system; I would prefer a manner in which the interaction of data are passed in memory, just to prevent accidentally writing to the same XML file." There is nothing stopping you from generating your XML as a string and passing that string to stdout so that it can read by .NET. This involves no use of the filesystem. Commented Sep 25, 2009 at 11:49

4 Answers 4

3

You could use a stream with PHP to STDOUT:

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

And then in .NET capture the output. I don't have much experience with .NET but this appears to allow you to capture the output of a process:

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

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer: I wish I could upvote you twice. The reason I can't accept your answer is that you are somehow lacking of a .net solution. A complete answer is provided below: stackoverflow.com/questions/1476206/…
1

The preferred way of passing data to different applications is XML. be it in the form of a file, or as ArsenMkrt mentioned, through a webservice.

Using this technique you are sure that almost any technology will be able to handle your data.

Comments

0

I don't know PHP, but I think it will have the feature to work with web services, write web service in .net that works with your net program and pass data from your php server to that service

Comments

-2

Improving on Inspire's solution, here's the complete code:

PHP:

<?php

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

And here's the .Net code:

[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);

}

1 Comment

This is the solution I used, I tested it to make sure that it at least worked on my machine, and yet I was downvoted?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.