0

我需要帮助来编辑这个 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 ......但我确定如果有人可以帮助我更改代码,它会一切正常!

谢谢

4

1 回答 1

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);

}

于 2012-11-01T04:17:16.940 回答