I am creating an API to recieve and process data from phone app using PHP. I can successfully process most calls but, am struggling with how to receive POST with image and json in it.
I have the following code in my API.php file:
//Make sure that it is a POST request.
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
throw new Exception('Request method must be POST!');
}
//Make sure that the content type of the POST request has been set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
throw new Exception('Content type must be: application/json');
}
//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
//Attempt to decode the incoming RAW post data from JSON.
$json = json_decode($content, true);
//If json_decode failed, the JSON is invalid.
if(!is_array($json)){
throw new Exception('Received content contained invalid JSON!');
}