douya7282 2014-03-26 08:15
浏览 72
已采纳

使用美元符号作为节点名访问PHP对象 - 解析错误:语法错误,(T_CONSTANT_ENCAPSED_STRING)

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.

  • 写回答

1条回答 默认 最新

  • douhan9619 2014-03-26 08:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?