从Web管理界面中提取数据

时间:2013-03-11 13:04:11

标签: php javascript web-applications

我希望从UPS的Web管理界面中提取状态字段,以便可以在我正在编写的另一个Web应用程序中使用该数据。我想知道是否有人知道如何解决这个问题,因为我似乎无法通过网络搜索找到我正在寻找的信息。我还需要刷新或重新检查的值。下面首先关闭在线字段的UPS网络界面示例:

Example of UPS Web interface

1 个答案:

答案 0 :(得分:1)

这是一个非常基本的例子,我没有测试过(php没有安装)。

您需要查看控制面板的来源,并了解如何识别包含所需信息的元素。

下面的代码(希望)搜索一个标识为server-status的元素,如果该元素存在,则检查其class属性以确定服务器的状态。

您不必使用dom内容,也可以使用正则表达式或其他内容。只要您能准确找到所需信息。

您可能还需要使用cURL或比file_get_contents()更高级的内容,因为您可能需要登录凭据才能查看相关页面。

<?php

    $html = file_get_contents("http://path.to/your/control.panel");
    // you may need to use cURL or something more advanced if you need to provide login credentials

    $dom = new DOMDocument;

    $dom->loadHTML($html);

    $test = $dom->getElementById('server-status');

    if ($test == NULL) {
        // unable to find element, somethings up!
    } else {
        if ($test->getAttribute('class') == "online") {
            // status element has "online" class, server is online
        } else {
            // status element does not have "online" class, somethings up!
        }
    }

?>

<强>更新

快速浏览一下该管理软件的演示,它不会像我的示例那么简单,因为似乎没有任何有用的元素idclass名称。它仍然可以做到。