How to Post JSON Data with PHP cURL

We create the correct JSON data, set that as the body of the POST request, and also set the headers correctly so that the server that receives this request will understand what we sent:


Explanation:

  • Basically the idea is to set the request to be a POST request.
  • Set the json-encoded data to be the body.
  • Set the correct headers to describe that post body.
  • The CURLOPT_RETURNTRANSFER is purely so that the response from the remote server gets placed in $result rather than echoed.

This content has been helpful to you?

Thanks for contributing!

Yes No