Skip to content

Instantly share code, notes, and snippets.

@sideshowcoder
Created February 14, 2012 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sideshowcoder/1826959 to your computer and use it in GitHub Desktop.
Save sideshowcoder/1826959 to your computer and use it in GitHub Desktop.
JSON POST Params in PHP
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Give Me Post Params
*
* Check if params are JSON encoded (acording to content-type header) and if so decode and return
* if not simply return the POST array so
*
* @access public
* @return array
*/
if ( ! function_exists('give_me_post_params'))
{
function give_me_post_params()
{
$result = $_POST;
// Check if Content Type is JSON
if( isset( $_SERVER['CONTENT_TYPE'] ) &&
strpos( $_SERVER['CONTENT_TYPE'], "application/json" ) !== false )
{
$jsonData = json_decode( trim( file_get_contents( 'php://input' ) ), true );
$result = $jsonData;
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment