如何在我的 php 脚本中回复 POST?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7501346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:49:20  来源:igfitidea点击:

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

phppost

提问by xbonez

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 脚本来接收 POST 信息,如下所示:

<?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?

如何让我的 php 脚本向调用它的页面/客户端发送回复?如果我想通过 C# 应用程序进行 POST 调用,我怎么知道 POST 是否成功?接收 POST 的 php 脚本可以发回响应吗?

采纳答案by danishgoel

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.

例如,如果您通过 Web 浏览器发出请求,则浏览器会显示响应。

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

如果您执行 AJAX 调用,则会在 javascript 回调函数中收到响应。

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

如果如您所说,您通过 C# 应用程序(通过例如 HttpWebRequest)调用它,那么在 HttpWebResponse 中收到的响应就是您在脚本中回显的内容

回答by Muhammad Usman

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

你正在使用echo它是正确的。也header()函数是用于先进的通信。

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

有关如何使用各种 HTTP 1.1 标头的详细信息,您可以查看链接

回答by PeeHaa

There are several ways to send back info.

有几种方法可以发回信息。

Basically you are already doing it by echoing some text.

基本上你已经通过echo输入一些文本来做到这一点。

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

或者您可以发回一个 html 页面或一个格式化的字符串(例如 JSON)。