2017-04-11 41 views
-1

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

这是json ...和解析json更容易..你想从那个文件得到什么。 –

+0

请问,api响应是json,你如何使用xml_ *库解析它? – hassan

+0

要以纯文本形式查看_JSON_(正如其他人指出的那样),只需浏览到url即可。我也猜测你没有检查错误日志或显示错误,因为这应该会引发一些警告。 –

回答

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数据,转换成数组以得到你需要具体的事情。

+0

@MagnusEriksson对不起那个粗心 –

+0

我想这个输出数据到表中,我创建了如下的表头: '

“;” 这将是正确的代码,用来举例 - ? 回声(” ​​‘$ users_data->胜’ 等等... – Ryan

+0

@Ryan它只是你如何让你可以修改。它适合你的需要 –

UFC卫冕冠军
名称 损失 重量级别