Php 从外部URL检索Json数据

Php 从外部URL检索Json数据,php,Php,我很难从API中获取一些json数据。基本上,我希望获得文件内容,然后将“meta_游戏”分配给一个变量,我还希望将“channel_count”分配给一个变量。正如您在这里看到的,我为这两个实例尝试了两种不同的方法,但目前都不起作用 $json = file_get_contents($chan); $json_data = unserialize($json); $gameName = json_decode($json)->meta_game; $viewerCount = $jso

我很难从API中获取一些json数据。基本上,我希望获得文件内容,然后将“meta_游戏”分配给一个变量,我还希望将“channel_count”分配给一个变量。正如您在这里看到的,我为这两个实例尝试了两种不同的方法,但目前都不起作用

$json = file_get_contents($chan);
$json_data = unserialize($json);
$gameName = json_decode($json)->meta_game;
$viewerCount = $json_data['channel_count'];

下面是一个URL示例,它将从:+channel name.获取此信息。

为什么要取消数据序列化?这不是必需的

$json = file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $channelName);
$jsonData = json_decode($json);

$gameName = $jsonData->meta_game;

$viewerCount = $jsonData->channel_count;
当然,您可能需要检查通过
$jsonData
对象的路径以找到最终值,因为我没有解析它,也不能保证它们位于第一个节点

更新:因为我不能放任自流,所以我检查了这个,看看API的响应是什么

当它首先返回一个数组时,您的路径将

$jsonData[0]->channel->meta_game;

$jsonData[0]->channel_count;
假设您在响应中只有一个通道,您可以尝试下面的代码

(function($){
    $.fn.fetch = function() {
        $.ajaxSetup({ cache: true });
        //var optionsWithDefaults = $.extend(defaults, options);
        return this.each(function() {
            var obj = [];
            $.getJSON(' http://api.justin.tv/api/stream/list.json?channel=xxx',
                function(data) {
                    $.each(data, function(i, feed) {
                        if(feed.channel_count !== undefined) {
                            obj.push(feed.channel_count);
                        }
                    });
                }
            );
        });
    };
})(jQuery);
<?php
$json = file_get_contents('http://api.justin.tv/api/stream/list.json?channel=');
$gameName = json_decode($json,true);
$new_arr = array();
foreach($gameName as $g){
print_r( $g['channel']['meta_game']);
 array_push($new_arr,$g['channel']['meta_game']);
}    
?>


您是否尝试运行干式测试以查看json_数据变量的回报?我不明白您怎么可能说出刚才所说的话。在这个网站上问一个问题怎么会被认为是懒惰的呢?很明显,我无法重现我所寻找的东西,这就是我来这里的原因。“这是我的代码。我不在乎它如何无法满足我的需求。猜猜是什么错了,然后修复它。”有没有其他方法可以从一个对这种语言没有你那么精通的用户的角度来提问?谢谢,我真的很感谢你的帮助。