Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Content-type POST request PHP

How can I check if the Content-type of a POST request in PHP is either application/json or application/x-www-form-urlencoded?

I've tried using $_SERVER["CONTENT_TYPE"] and echo get_headers('url', 1)["Content-Type"] but neither of those work for me.

like image 871
user3182261 Avatar asked May 27 '26 08:05

user3182261


2 Answers

echo '<pre>';
print_r(getallheaders());

So

$allHeaders = getallheaders();
$contentType = $allHeaders['Content-Type'];
like image 166
Dennis Avatar answered May 31 '26 11:05

Dennis


It's working below codes. You can try it

$headerArray = getallheaders();
$contentType = $headerArray['Content-Type'];
(Or)
$contentType =  $_SERVER["CONTENT_TYPE"];
(Or)
$headerArray = apache_request_headers(); 
$contentType = $headerArray['Content-Type'];
like image 33
smdhkv Avatar answered May 31 '26 11:05

smdhkv