使用PHP从JSON文件获取API数据

时间:2016-02-12 16:20:29

标签: php json

我使用API​​进行锦标赛服务,但我不知道如何从json-text到php变量获取数据。

这是制作json文本的PHP代码:

$header_size    = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header         = substr($output, 0, $header_size);
$body           = json_decode(substr($output, $header_size));

这是我的json文本:

array(8) 
{ 
    [0]=> object(stdClass)#1 (12) {    
        ["id"]=> string(24) "5666e0ef150ba051458b456b" 
        ["discipline"]=> string(16) "counterstrike_go" 
        ["name"]=> string(18) "A csgo tournament #1" 
        ["full_name"]=> string(18) "A csgo tournament #1" 
        ["status"]=> string(9) "completed" 
        ["date_start"]=> string(10) "2015-12-12" 
        ["date_end"]=> string(10) "2015-12-13" 
        ["online"]=> bool(false) 
        ["location"]=> string(19) "Online" 
        ["country"]=> string(2) "SE" 
        ["size"]=> int(32) 
        ["archived"]=> bool(false) 
    } 
    [1]=> object(stdClass)#2 (12) { 
        ["id"]=> string(24) "5652cc7c150ba0e3568b4567" 
        ["discipline"]=> string(15) "leagueoflegends" 
        ["name"]=> string(16) "A csgo tournament #1" 
        ["full_name"]=> NULL 
        ["status"]=> string(5) "setup" 
        ["date_start"]=> string(10) "2015-12-11" 
        ["date_end"]=> string(10) "2015-12-12" 
        ["online"]=> bool(false) 
        ["location"]=> string(8) "Online" 
        ["country"]=> string(2) "SE" 
        ["size"]=> int(16) 
        ["archived"]=> bool(false) 
    } 
    [2]=> object(stdClass)#3 (12) { 
        ["id"]=> string(24) "555dd064140ba040588b456e" 
        ["discipline"]=> string(16) "counterstrike_go" 
        ["name"]=> string(30) "A csgo tournament #1" 
        ["full_name"]=> string(30) "A csgo tournament #1" 
        ["status"]=> string(7) "running" 
        ["date_start"]=> string(10) "2015-06-05" 
        ["date_end"]=> string(10) "2015-06-05" 
        ["online"]=> bool(true) 
        ["location"]=> string(12) "#Online" 
        ["country"]=> string(2) "SE" 
        ["size"]=> int(128) 
        ["archived"]=> bool(false) 
   } 
}

1 个答案:

答案 0 :(得分:0)

循环并打印每场比赛做类似的事情

for ($i = 0; $i < count($body); $i++) {
    echo "$body[0]->'name'";
}

希望有所帮助!