doudu2591 2015-04-07 17:06
浏览 72
已采纳

json_decode for twitch api

Well, I am trying to use json_decode to get a users twitch name via their steam ID, however I am getting an error, and I have read other users issues and I am no closer to fixing it.

Here is my code:

$getcontents = file_get_contents('http://api.twitch.tv/api/steam/76561198049928469');
var_dump(json_decode($getcontents));
$twitchname = $getcontents ['name'];
echo $twitchname;

Here is my error:

Warning: Illegal string offset 'name' in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\Portfolio -- Website\forum\index.php on line 29

I have looked at documentation on the dev forums on twitch and I cannot find a way to fix this.

I have also looked at answers on these forums and I cannot find a way to fix this.

Forgot this; vardump output:

object(stdClass)#2 (2) { ["_id"]=> int(59956494) ["name"]=> string(10) "riggster98" } 
  • 写回答

2条回答 默认 最新

  • dongli2000 2015-04-07 17:10
    关注

    So the way it works with json_decode is, if you intend to use it like a normal php variable, then you have to pass the true flag into the json_decode function. Then you can use $content['name'] and get the expected results.

    But if you want to work with Objects, you can simply just json_decode the content, and then use $content->name to extract the content.

    Like this

        $getcontents = file_get_contents('http://api.twitch.tv/api/steam/76561198049928469');
    
        $contents = json_decode($getcontents);
    
        echo "<pre>";
        print_r($contents->name);
        echo "</pre>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型