通过简单的html dom解析器将带有API的URL解析为数组


Parse url with API to an array via simple html dom parser

我在启动"氏族社区统计"项目时遇到问题。我有一个带有统计信息 API 的 URL,我想使用简单的 html dom 解析器在数组中显示它。我想得到这个效果:

{
  "status": "ok", 
  "count": 1, 
  "data": {
    "1": {
      "members_count": 100, 
      "description": "Закрытый клан...", 
      "description_html": "<p>Закрытый клан....'n</p>", 
      "created_at": 1293024672, 
      "updated_at": 1375930001, 
      "name": "Wargaming.net", 
      "abbreviation": "WG", 
      "emblems": {
        "large": "http://cw.worldoftanks.ru/media/clans/emblems/clans_1/1/emblem_64x64.png", 
        "small": "http://cw.worldoftanks.ru/media/clans/emblems/clans_1/1/emblem_24x24.png", 
        "medium": "http://cw.worldoftanks.ru/media/clans/emblems/clans_1/1/emblem_32x32.png", 
        "bw_tank": "http://cw.worldoftanks.ru/media/clans/emblems/clans_1/1/emblem_64x64_tank.png"
      }, 
      "clan_id": 1, 
      "members": {
        "196632": {
          "created_at": 1293126248, 
          "role": "private", 
          "updated_at": 1375930001, 
          "account_id": 196632, 
          "account_name": "Wrobel"
        }, 
        "18458": {
          "created_at": 1360836543, 
          "role": "diplomat", 
          "updated_at": 1375930001, 
          "account_id": 18458, 
          "account_name": "alienraven"
        }, 
        "3100": { ....
        }
      }, 
      "motto": "Орлы! Орлицы!", 
      "clan_color": "#e18000", 
      "owner_id": 1277137
    }
  }
}

我的代码

include('simple_html_dom.php');
$html = file_get_html('http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500009659');
...?

接下来我应该怎么做才能准备一个数组并以我想要的方式在我的网页上显示它?有人有义务向我解释吗?请。

问候玛丽

试试这个

<?php
// example of how to modify HTML contents
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500009659');
$data = json_decode($html);
echo "<pre>";
print_r($data);
?>

我看到数据是json格式。只需使用:

$json  =  file_get_contents('http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500009659'); //gets the json
$obj = json_decode($json); //decode the json into object or array. You'll get an object
var_dump($obj); //view the object. Parse the object the way you want to.