0

I have a server which accepts HTTP Post request with JSON body and sends response in JSON. I dont know any way in PHP to do this.

I need to send a request to server with the JSON body

{
MyName="Hitesh"
MyPhone="8008288081"
}

Any help will be highly appreciated.

The API is available in a server hosted at xyz.hitesh.com and the PHP code will be hosted at abc.test.com

1 Answer 1

6
$requestBody = file_get_contents('php://input');
$requestBody = json_decode($requestBody);

// ???

$response = array('Accepted' => 'Yes');
header('Content-Type: application/json');
echo json_encode($response);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your quick reply, I am relatively new to PHP, can you help me with some more details.
$requestBody = file_get_contents('php://input'); What is this php://input
@Hiteshwar It's a stream wrapper for whatever input PHP received, e.g. the body of a POST request. Think of it as a virtual file path, like file:///path/to/some/file, just that it doesn't really exist as a file.

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.