JSON POST Requests: $_POST Won’t  Work

PHP's $_POST won't work with a content-type of application/javascript.

Receiving JSON POST data in PHP is a common scenario I come across when building custom WordPress API’s. If you’re working with JSON API’s powered by PHP you may find that $_POST is not working with JSON, and you may be seeing a broken response of Array or NULL.

Processing $_POST data with application/javascript headers

To receive a request with a content-type of application/javascript in PHP you’ll need to use the following:

$data = json_decode( file_get_contents( 'php://input' ), true );

This is really for my own reference/editor integration, but hopefully it’s helpful to you as well!

Related Articles

Meet the Author

Kevin Leary, WordPress Consultant

I'm a custom WordPress web developer and analytics consultant in Boston, MA with 17 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.