PHP检查传入请求是否为JSON类型 [英] PHP check whether Incoming Request is JSON type

查看:153
本文介绍了PHP检查传入请求是否为JSON类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,是否要检查传入请求是否为AJAX JSON类型?

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

我尝试过

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

但这没用.

有什么想法吗?

推荐答案

您确切在哪里接受您不知道的请求?

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

您可能在脚本的开头有一个函数,尝试将数据作为JSON或simplexml导入.如果发现错误,您就知道是另一个...

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...

再三考虑一下,是否将其测试为JSON,出于多种原因,simplexml将抛出错误.

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;

这篇关于PHP检查传入请求是否为JSON类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆