【问题标题】:Access json post data with php用php访问json post数据
【发布时间】:2016-01-14 06:07:19
【问题描述】:

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

这是我的代码,如果我朝着正确的方向前进,请告诉我

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 数据。

【问题讨论】:

  • 试试var_dump($postData)看看数组包含什么。

标签: php json post sendgrid


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    相关资源
    最近更新 更多