1

我试图让这个 XML 的内容位于http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=W1AW(我以 W1AW 呼号为例)显示在一个表(不是每个条目,只有“licName”、“callsign”、“serviceDesc”、“statusDesc”和“expiredDate”。)

在我的网站上,我希望用户能够根据 url parremeter ?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 文档

4

2 回答 2

10

基于 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>
于 2013-06-05T04:02:29.673 回答
4

那么基本上你可以使用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>';

并遍历您获得的对象以显示页面。

于 2013-06-05T03:37:03.273 回答