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


Get with a API data from JSON-file with PHP

我使用锦标赛服务的 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) 
   } 
}

要循环和打印每个锦标赛,请执行类似操作

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

希望对您有所帮助!