I have a program that will make a web request to a php page (there will not be a browser involved here), and I need to send some data using php back to the program... how do I do this?
Just use echo to echo the thing you want sent back.
<?php
echo "thing that i want to send back";
?>
Have you considered a webservice? It might be overkill. Otherwise.. a POST or GET and echoing the response with PHP..?
What you're describing is basically a website.
post and get really seem like the solution here. Although I am not very comfortable with the technique because it is prone to errors if parameters in the URL are incorrect or not validated. Maybe consider something other than the PHP? but hey the answer is that it would work like the others said above :)
Depending on the type of data you need returned from PHP, and assuming you have control of both the C# application and the PHP application, you will most likely use something such as the WebClient
class, which allows you to make an HTTP call to a URL, which you would point to your PHP application. Once the request is made, you do whatever processing is necessary in your PHP application, and then most likely echo
it back to the C# application (also, depends on the type of data you're using)
A simple HTTP response consist of headers and body. The headers are not displayed in your web browser, but take place in the client(browser) server communication (web server) - simply said.
The web server will return to your program also so named headers, for which you will have to take care about.
Using your php script you can sent your output with echo
:
<?php
echo 'Responding to the program...';
?>
You should echo what you want to return, like the others said.
for c# application, you should wait for response, and read the response stream to get the echos.
public GetResponeFromPHP()
{
HttpWebRequest request = null;
HttpWebResponse resp = null;
try
{
request = (HttpWebRequest)WebRequest.Create("http://www.URL_of_php.php");
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
// execute the http request and get the response
resp = request.GetResponse() as HttpWebResponse;
if (resp.StatusCode == HttpStatusCode.OK)
{
//read the response stream
String responseText;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
responseText = sr.ReadToEnd();
}
// Now use respones text to get the echos //
}
}
catch (Exception e)
{
throw e;
}
finally
{
if (resp != null)
resp.Close();
}
}