请帮我调整此脚本以加载外部XML而不是本地

时间:2012-11-01 03:19:18

标签: php xml

我需要帮助来编辑这个PHP脚本。目前它在本地服务器上查找最新的XML文件,但我希望它从另一台服务器加载一个特定的XML文件。

    define('XML_LOCATION', "../"); //relative to this file

... ...

function load_xpath($dir=XML_LOCATION) {
    if ($_REQUEST[SORTORDER] == DEFAULTSORT) {
        //$dom = new DOMDocument;
        //$dom->load($this->latest_xml_file($dir));
        $dom = $this->load_xslt($this->latest_xml_file($dir), $this->xslprops[LENGTHDESC][XSL], $this->xslprops[LENGTHDESC][SORT]);
    }
    else {
        $dom = $this->load_xslt($this->latest_xml_file($dir), $this->xslprops[$_REQUEST[SORTORDER]][XSL], $this->xslprops[$_REQUEST[SORTORDER]][SORT]);
    }
    $xp = new DOMXPath($dom);
    $xp->registerNamespace(NS, "http://www.starstandard.org/STAR/5");
    return($xp);
}

function load_xslt($xml, $xsl="", $sort_order="ascending") {
    $domxml = new DOMDocument;
    $domxsl = new DOMDocument;
    if ($domxml->load($xml) && $domxsl->load($xsl)) {
    //if ($domxml->loadXML($xml) & $domxsl->loadXML($xsl)) {
        $proc = new XSLTProcessor();
        $proc->importStylesheet($domxsl);
        $proc->setParameter(NS, 'sortorder', $sort_order);
        return $proc->transformToDoc($domxml);
    }
    else {
        //TODO: decide on error out method
    }
}

function latest_xml_file ($dir=XML_LOCATION) {
    $xml_list = array();
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                if (preg_match('/.*\.xml$/i', $file)) {
                    array_push ($xml_list, $dir.$file);
                }
            }
            closedir($dh);
        }
    }

    rsort($xml_list);
    return $xml_list[0];
}

我确定不需要很多代码,因为我不需要它来计算最新文件,只需加载外部文件。
例如:https://services.boatwizard.com/bridge/events/ae0324ff-e1a5-4a77-9783-f41248bfa975/boats将是外部XML。

我真的不知道php或xml ...但我确定如果有人可以帮我改变代码,那么一切正常!

由于

1 个答案:

答案 0 :(得分:0)

这样的事情:

$remoteFile = file_get_contents('https://services.boatwizard.com/bridge/events/ae0324ff-e1a5-4a77-9783-f41248bfa975/boats'); 

function load_xpath($remoteFile) {
    $dom = $this->load_xslt($remoteFile, $this->xslprops[LENGTHDESC][XSL], $this->xslprops[LENGTHDESC][SORT]);
    $xp = new DOMXPath($dom);
    $xp->registerNamespace(NS, "http://www.starstandard.org/STAR/5");
    return($xp);

}