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?
$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, );
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With