简体   繁体   中英

How do I reply to a POST in my php script?

Somehow, I have a feeling my question is very straightforward and noobish, but I tried googling and couldn't figure it out. Perhaps I didn't know the right keywords.

Anyways, I have a short php script to receive POST info as below:

<?php
    if (isset($_POST['name']) && isset($_POST['info']))
    {
        echo "<strong>Post received.</strong> <br/> <br/> <strong>Name:</strong> " . $_POST['name'] . "<br/><strong>Info:</strong> " . $_POST['info'];
    }
    else
    {
        echo "Post not received.";
    }
?>

How can I have my php script send a reply to the page / client that calls it? If I want to make the POST call via, say a C# app, how can I know if the POST was successful? Can my php script that is receiving the POST send back a response?

The response is sent to the caller only, So it doesnt matter through which channel you call a script (whether with/without parameters).

For example if you make the request via a web browser the response is shown by the browser.

If you do an AJAX call then the response is recieved in the javascript callback function.

And if as you say you call it via a C# app (via eg HttpWebRequest), then the responce recieved in HttpWebResponse is what you echo in your script

You are using echo and its correct. Also header() is used for advanced communication.

For details on how to use various HTTP 1.1 headers you may see the link .

There are several ways to send back info.

Basically you are already doing it by echo ing some text.

Or you can send back a html page or a formatted string (eg JSON).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM