Access a PHP-object with dollar-sign as node name - Parse error: syntax error, (T_CONSTANT_ENCAPSED_STRING)

StackOverflow https://stackoverflow.com/questions/22654997

  •  21-06-2023
  •  | 
  •  

Question

Before we get into this, I've found the exact issue i'm dealing with, however the solution does not fix my issue.

Access a PHP-object with dollar-sign as node name

Here is the relevant PHP code.

$user = 'officialtiesto';

$artist_json = file_get_contents('https://gdata.youtube.com/feeds/api/users/' .  $user  . '?alt=json');
$artist_object = json_decode($artist_json, FALSE);

var_dump($artist_object->'yt$googlePlusUser');

I have also tried this:

var_dump($artist_object->{'yt$googlePlusUser'})

Both present me with the following error:

Parse error: syntax error, unexpected ''yt$googlePlusUser'' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in C:\Users\astark\Desktop\charts\youtube.php on line 22

I read somewhere that having an out of date version of PHP can cause issues similar to this so i've included a link to a JSBin (http://jsbin.com/yahevoyo) with the specs of the XAMPP setup i'm running. Pretty stumped here on this one and not sure if its just the late night getting to me, or a larger problem. Please advise.

Was it helpful?

Solution

First, there is no yt$googlePlusUser, it's yt$googlePlusUserId.
Then you've missed one level. Instead of

$artist_object->{'yt$googlePlusUserId'}

use

$artist_object->entry->{'yt$googlePlusUserId'}

And finally, as a bonus, to get the ID:

$artist_object->entry->{'yt$googlePlusUserId'}->{'$t'}

Alternatively as it's written in the answer to the question you referred to you could convert the JSON object to an array using $artist = json_decode($artist_json, true) and access the property as $artist['entry']['yt$googlePlusUserId']['$t].

Update:
Regarding PHP version, etc. you seem to have 5.4.19 installed, I've tested the above on 5.4.0 and it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top