0

This is the JSON i am trying to decode with PHP, but getting issues. Please check my code below.

{ "146505212039213_2962095710480135": {
  "reactions_like": {
     "data": [
     ],
     "summary": {
        "total_count": 172595
     }
  },
  "reactions_love": {
     "data": [
     ],
     "summary": {
        "total_count": 75252
     }
  },
  "reactions_haha": {
     "data": [
     ],
     "summary": {
        "total_count": 132
     }
  },
  "id": "146505212039213_2962095710480135"
}}

Code i am using:

function curl_get_contents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$json = curl_get_contents('URL HERE');
$data = json_decode($json);
echo $data->146505212039213_2962095710480135->reactions_like->summary->total_count;

This is the error i am getting:

Parse error: syntax error, unexpected '146505212039213' (T_DNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in C:\xampp\htdocs\test.php on line 13

What i am doing wrong here?

Vega
  • 27,856
  • 27
  • 95
  • 103
  • The error sort of gave the answer away: "expecting identifier (T_STRING)". Things like `12_34` are not numeric. Look on how to [access properties with special characters](https://duckduckgo.com/?q=php+access+property+with+special+characters) – mario Sep 01 '17 at 20:14
  • Reopened because there's no good answer for T_DNUMBER in the wiki post. – Sammitch Sep 01 '17 at 20:15

2 Answers2

1

Well, that error message is really helpful here. A parser waits for either a property name (which should be a valid identifier - and this eliminates digit as a first symbol), or $ character (followed by identifier expression), or {. And yet it receives a number - and stops, dazed and confused.

To use your key as a property name, you should turn it into a string. One possible way is using {' ... '} wrapper:

echo $data->{'146505212039213_2962095710480135'}->...

Demo. Alternative, you can store this string in a variable and use that variable instead:

$index = '146505212039213_2962095710480135';
echo $data->$index->...

Still I'd strongly encourage you to consider another approach: use associative arrays instead of objects. A particular reason for this is a well-known issue of accessing numeric properties in objects.

$json = '{"146505212039213_2962095710480135":"test"}';
$data = json_decode($json, true); // use array, not object
echo $data['146505212039213_2962095710480135']; // test
raina77ow
  • 103,633
  • 15
  • 192
  • 229
0

Identifiers/variable names can't begin with a number, so $data->146505212039213_2962095710480135 is invalid.

You'll either need to do something like:

$index = '146505212039213_2962095710480135';
echo $data->$index->reactions_like->summary->total_count;

or

echo $data->{'146505212039213_2962095710480135'}->reactions_like->summary->total_count;
Sammitch
  • 30,782
  • 7
  • 50
  • 77