我正在写一个脚本,注册为一个webhook的端点。我知道它已成功注册,因为我正在将每个请求的标头写入服务器日志。下面是一个示例:
Content-Type: text/xml; charset=UTF-8
User-Agent: Jakarta Commons-HttpClient/3.1
Host: =={obfuscated}==
Content-Length: 1918
我注册的API是post JSON对象到我的脚本,我想用PHP解析该对象。从请求头中可以看到,有一个非常大的JSON对象等待解析。这看起来很简单,但事实并非如此。
起初我尝试使用$_POST['json']
或$_POST
,但由于数据不在数组中,我真的不确定如何访问它。
我试过使用file_get_contents('php://input')
和fopen('php://input', 'r')
有和没有json_decode()
,但没有运气。我不能使用http_get_request_body()
,因为我所在的服务器没有PECL,这是我无法控制的。
是否有其他方法与POST-ed JSON对象交互,我错过了?谢谢!
我只需要
$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE); //convert JSON into array
,其中json_decode
中的第二个参数返回对象为数组
即使下面的工作。
$inputJSON = file_get_contents('php://input');
如果你想继续使用$_POST发送数据作为FormData
var fd = new FormData();
fd.append('key', 'value');
return axios.post('url', fd)