user3182261
user3182261

Reputation: 271

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.

Upvotes: 3

Views: 6246

Answers (2)

smdhkv
smdhkv

Reputation: 187

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'];

Upvotes: 1

Dennis
Dennis

Reputation: 394

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

So

$allHeaders = getallheaders();
$contentType = $allHeaders['Content-Type'];

Upvotes: 6

Related Questions