Receiving JSON POST data via PHP

If you get Json Post data in php, you must use:

$content = file_get_contents("php://input");

Below example code:

<?php

//Check whether the request is POST
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
    return false;
}

//Check content type is application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
   return false;
}

//Get RAW post data.
$content = trim(file_get_contents("php://input"));

//Decode RAW data.
$decoded = json_decode($content, true);

//If json_decode failed, the JSON is invalid.
if(!is_array($decoded)){
  return false;
}

var_dump($decoded)
  
//carminemilieni.it

Leave a Reply

Your email address will not be published. Required fields are marked *