如何在PHP中修复“未定义索引”

时间:2019-08-26 16:42:13

标签: php html api curl

我正在从使用php的shoppy.gg API设置反馈页面。 我从创建一个简单的curl请求开始:

<?php
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://shoppy.gg/api/v1/feedbacks/",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array(
        "Authorization: PRIVATE KEY",
        "User-Agent: Feedbacks"
    )
));

$feedbacks = curl_exec($ch);
echo $feedbacks;
curl_close($ch);
?>

此代码有效,并且为我提供了购物页面的所有反馈的列表,如下所示:

{
    "id": "004db0e0-261c-11e8-aa86-55b1affdff42",
    "order_id": "10305ca0-26a5-11e8-a449-6706fd565ec5",
    "secret": "GNCCqh4QHHhZDL8o2EFBkfqyVm6uZKMr",
    "comment": "Very good product, fast replies",
    "stars": 5,
    "rating": 1,
    "response": "Thanks for the feedback",
    "created_at": "2018-03-12 16:37:14",
    "updated_at": "2018-03-12 16:37:14",
    "product": {
        "id": "7729b540-fd3e-11e7-9c7e-a15b58798da8",
        "title": "Another Product"
    }
}

现在我要做的是为每个反馈导出一个变量,但我不知道如何。我这样尝试过:

<?php
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://shoppy.gg/api/v1/feedbacks/",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array(
        "Authorization: PRIVATE KEY",
        "User-Agent: Feedbacks"
    )
));
$feedbacks_json = curl_exec($ch);
curl_close($ch);

$feedbacks = json_decode($feedbacks_json, true);
?>

<p><?php echo $feedbacks['comment'] ?></p>

但这给我一个错误

Notice: Undefined index: comment in C:\xampp\htdocs\FB\index.php on line 19

我不知道如何解决它,然后如何从单个反馈中提取单个变量。

希望这很清楚。

0 个答案:

没有答案