Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

post

php

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?

like image 469
xbonez Avatar asked Sep 21 '11 14:09

xbonez


People also ask

How to get POST response in PHP?

$options = array( 'header' => "Connection: close\r\n". "Content-Length: ". strlen($query)."\r\n". "Content-Type: application/x-www-form-urlencoded\r\n", // add this line 'method' => 'POST', 'content' => $query, );

How send POST request in PHP?

We can send the POST request in PHP by using the functions like http_build_query() , stream_context_create() and file_get_contents() functions without using the CURL. We can use the http_build_query() function to create query parameters to send in the POST request.

How to POST and get data in PHP?

GET can't be used to send binary data, like images or word documents, to the server. The data sent by GET method can be accessed using QUERY_STRING environment variable. The PHP provides $_GET associative array to access all the sent information using GET method.

How to get POST data in PHP api?

Sample API POST Request Example [PHP Code] To make a POST request to an API endpoint, you need to send an HTTP POST request to the server and specify a Content-Type request header that specifies the data media type in the body of the POST request.


2 Answers

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 e.g. HttpWebRequest), then the responce recieved in HttpWebResponse is what you echo in your script

like image 176
danishgoel Avatar answered Oct 24 '22 05:10

danishgoel


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.

like image 27
Muhammad Usman Avatar answered Oct 24 '22 07:10

Muhammad Usman