2

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?

1
  • 1
    If you don't mind, try to include code and worst case psuedocode when raising issues. It just helps others in future references too :) Commented Jan 5, 2012 at 19:14

6 Answers 6

3

Just use echo to echo the thing you want sent back.

<?php
   echo "thing that i want to send back";
?>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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();
        }

    }

2 Comments

Your comments don't really add anything to the code, stating ` //Declare an HTTP-specific implementation of the WebRequest class` is obvious from the code. Comments should explain how and why, if needed
@diggingforfire: Thanks, you're right, I took an example from one of my old apps, and forget to remove them. Fixed!
0

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 :)

Comments

0

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)

Comments

0

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...';
?>

Comments

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.