繁体   English   中英

使用php访问json发布数据

[英]Access json post data with php

我正在编写一个小型PHP应用程序,它将从Send Grids Webhook API中获取发布数据,但看起来它像是在发送json作为发布数据。 我不确定如何访问该数据。 我以前使用过post数据,但是我使用$ _POST访问它,但从未收到json post数据。

这是我的代码,让我知道我是否朝着正确的方向前进

include 'send_grid_conn.php';
$dealer = (isset($_GET['dealer']) && !empty($_GET['dealer']))?$_GET['dealer']:"N/A";
echo $dealer;

$postData = json_decode($HTTP_RAW_POST_DATA,true);

$email = (isset($postData['email']))?$postData['email']:"nothing";

    $stmt = $connection->prepare("INSERT INTO `send_grid`(`email`, `dealer`) VALUES (?,?)");
    $stmt->execute(array($email, $dealer));

插入部分有效,但我无法访问POST数据。

我将清理代码顺便说一句。 现在,我只是在测试模式下尝试访问该json数据。

$HTTP_RAW_POST_DATA从php 5.6开始不推荐使用,从php 7开始被删除,您应该使用php://input因为它不依赖任何php.ini配置,并且,您是否尝试解码$_POST吗? 在请求中,json只是一个字符串,因此您应该没有任何问题。

php://input

json_decode(php://input, true);

使用$_POST

json_decode($_POST, true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM