PHP check whether Incoming Request is JSON type

前端 未结 6 1097
轻奢々
轻奢々 2020-12-20 14:57

Is there anyway to check whether an incoming request is of AJAX JSON type?

I tried

if(($_SERVER[\'REQUEST_METHOD\']==\'JSON\'))
{
}

6条回答
  •  萌比男神i
    2020-12-20 15:43

    Where are you accepting requests from, exactly, that you wouldn't know?

    You could have a function at the beginning of the script that tries to import the data as JSON or simplexml. If it catches an error, you know it's the other one...

    On second thought, have it test it to be JSON, simplexml will throw an error for tons of reasons.

     $json_request = (json_decode($request) != NULL) ? true : false;
    

提交回复
热议问题