繁体   English   中英

如何从 json 中获取值

[英]How to get a value from a json

我必须从 facebook 发送给我的 json 中获取“media_id”值,我尝试了所有方法,但我做不到。 我能怎么做?

<?php


$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

// Set this Verify Token Value on my Facebook App
if ($verify_token === 'token') {

echo $challenge;

$readjson = file_get_contents('php://input');

//Decode JSON
 $data = json_decode($readjson, true);

//try get media_id value

 print_r($data[entry][changes][value][media_id]);

 //response code 200 - ok
 header('HTTP/1.0 200 OK');

} else {
  header('HTTP/1.0 401 Unauthorized');
}




 ?>

这是发送给我的 json,我无法获取值“media_id”

{
    "object": "instagram",
    "entry": [{
        "id": "0",
        "time": 1573295665,
        "changes": [{
            "field": "story_insights",
            "value": {
                "media_id": "17887498072083520",
                "impressions": 444,
                "reach": 44,
                "taps_forward": 4,
                "taps_back": 3,
                "exits": 3,
                "replies": 0
            }
        }]
    }]
}

我感谢你给我的帮助

Please use key value in quotes, also you are not using indexes for arrays Please go though php manual for json travesing using indexes and keys

$json = <<<XML
{
    "object": "instagram",
    "entry": [{
        "id": "0",
        "time": 1573295665,
        "changes": [{
            "field": "story_insights",
            "value": {
                "media_id": "17887498072083520",
                "impressions": 444,
                "reach": 44,
                "taps_forward": 4,
                "taps_back": 3,
                "exits": 3,
                "replies": 0
            }
        }]
    }]
}
XML;
$json = json_decode($json,true);
print_r($json["entry"][0]["changes"][0]["value"]["media_id"]);

在 PHP 中,没有 $ 的变量是常量,而带有 $ 的是普通变量,因此您的键被误认为是常量变量

暂无
暂无

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

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