简体   繁体   中英

How to get a value from a json

I have to get the "media_id" value from the json that is sent to me from facebook, I tried everything but I can't. how can I do?

<?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');
}




 ?>

this is the json that is sent to me, I can't get the value "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
            }
        }]
    }]
}

I thank you for the help you give me

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"]);

in PHP variables without $ are constants and with $ are normal variables so your keys are bieng mistaken for constant variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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