我试图让这个XML的内容位于http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=W1AW(我使用W1AW呼号为例)显示在一个表中(不是每个条目,只有"licName", "呼号","serviceDesc", "statusDesc"answers"expiredDate")
在我的网站上,我希望使用能够选择什么呼号搜索基于url参数?callsign=choicehere(例如:mywebsite.com/callsignresults.php?callsign=w1aw)
我目前有以下代码。我只使参数工作。
<?php
$file = file_get_contents('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=' . ($_GET["callsign"]));
echo $file;
?>
那么如何将XML数据转换为用户友好的可读表呢?
表示例
| NAME | CALL SIGN | TYPE | STATUS | EXPIRATION DATE |
--------------------------------------------------------------
| John Doe | XXXX | Amature | Active | 1/1/13 |
| Jane Doe | X1X1X | Amature | Expired | 1/1/13 |
API文档根据Neta Meta的回答:
我在迭代构建的XML对象时遇到了一些麻烦,直到我意识到返回的XML的大小写不一致。下面是一些将该文件解析为表的工作代码:
<?php
$xml = new SimpleXMLElement('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue='.$_GET["callsign"], 0, TRUE);
?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Call Sign</th>
<th>Type</th>
<th>Status</th>
<th>Expiration Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($xml->Licenses->License as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->licName; ?></td>
<td><?php echo $licenseElement->callsign; ?></td>
<td><?php echo $licenseElement->serviceDesc; ?></td>
<td><?php echo $licenseElement->statusDesc; ?></td>
<td><?php echo $licenseElement->expiredDate; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
基本上你可以使用SimpleXML:
$file = file_get_contents('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=Verizon+Wireless');
$movies = new SimpleXMLElement($file);
echo '<pre>';
print_r($movies);
echo '<pre>';
并遍历显示页面的对象