使用php访问json发布数据

时间:2015-10-15 17:00:02

标签: php json post sendgrid

我正在编写一个小型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数据。

我要清理代码btw。现在我只是在测试模式下试图访问json数据。

1 个答案:

答案 0 :(得分:0)

$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);