Created
February 14, 2012 13:59
-
-
Save sideshowcoder/1826959 to your computer and use it in GitHub Desktop.
JSON POST Params in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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