http://ufc-data-api.ufc.com/api/v1/us/fighters/title_holders
我正在尝试解析上面链接中的数据,我希望在开始之前查看以纯文本显示的信息,因为我觉得这样可以更容易阅读?我不知道如何做到这一点,我之前做过类似的事情,但是从很久以前开始,所以我试图让自己恢复精神。
任何帮助都将不胜感激。
到目前为止我的代码是:
<?php
//Calling URL XML
$urltitleholders = ("http://ufc-data-api.ufc.com/api/v1/us/fighters/title_holders");
//Loading URL as XML
$titleholders_array = simplexml_load_file($urltitleholders);
foreach ($titleholders_array as $ufcth) {
echo "<pre>";
echo $ufcth;
echo "</pre>";
}
答案 0 :(得分:1)
<?php
$url = "http://ufc-data-api.ufc.com/api/v1/us/fighters/title_holders";
$data = file_get_contents($url);
$users_data = json_decode($data, true);
$error = json_last_error();
$data_to_get = ["wins", "losses", "first_name", "last_name", "weight_class"];
if (!$error) {
$table = "<table><tr> <th>Name</th> <th>Wins</th> <th>Losses</th> <th>Weight Class</th> </tr>";
foreach ($users_data as $user_data) {
$table .= "<tr><td>" . $user_data['first_name'] . " " . $user_data['last_name'] . "</td><td>" . $user_data['wins'] . "</td><td> " . $user_data['losses'] . "</td><td> " . $user_data['weight_class'] . "</td></tr>";
}
$table . "</table>";
echo $table;
} else {
echo "Error occured : $error";
}
以下是从json获取数据的方法,转换为数组以获取所需的特定内容。