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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘