如何点击网址并使用php下载其xml

时间:2015-03-02 04:03:27

标签: php xml json curl

我尝试了一切从网址获取xml,甚至是来自PHP How to hit a url and download its xml的smottt想法,但是对我来说没有用。

我的情景;

生成美元汇率的网址:

nrb.org.np/exportForexXML.php?YY=2015&MM=03&DD=01&YY1=2015&MM1=03&DD1=01

此处: YY MM DD 是开始日期, YY1 MM1 DD1 是报告的结束日期。我相信,它在亚洲加德满都的unix时间生成一个xml。每次和第二个单独的xml文件名。

搜索互联网,但没有,

我想使用php在页面中显示xml的结果,方法是将xml从给定的url下载到我的localhost文件夹或直接从web。

请帮忙。

先谢谢

已编辑:我正在使用的代码是

$url = "nrb.org.np/exportForexXML.php?YY=2015&MM=03&DD=01&YY1=2015&MM1=03&DD1=01";
$xml = new SimpleXMLElement($url, null, true);

foreach($xml->CurrencyConversionResponse as $CurrencyConversionResponse) {
    echo $CurrencyConversionResponse->BaseCurrency . "<br />";
    echo $CurrencyConversionResponse->TargetCurrency . "<br />";
    echo $CurrencyConversionResponse->ConversionTime . "<br />";
    echo $CurrencyConversionResponse->ConversionRate . "<br />";

}

错误信息是

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: I/O warning : failed to load external entity "nrb.org.np/exportForexXML.php?YY=2015&MM=03&DD=01&YY1=2015&MM1=03&DD1=01" in C:\xampp\htdocs\xml.php on line 4

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\xampp\htdocs\xml.php:4 Stack trace: #0 C:\xampp\htdocs\xml.php(4): SimpleXMLElement->__construct('nrb.org.np/expo...', 0, true) #1 {main} thrown in C:\xampp\htdocs\xml.php on line 4

1 个答案:

答案 0 :(得分:1)

http://添加到网址

$url = "http://nrb.org.np/exportForexXML.php?YY=2015&MM=03&DD=01&YY1=2015&MM1=03&DD1=01";
$xml = new SimpleXMLElement($url, null, true);

foreach($xml->CurrencyConversionResponse as $CurrencyConversionResponse) {
    echo $CurrencyConversionResponse->BaseCurrency . "<br />";
    echo $CurrencyConversionResponse->TargetCurrency . "<br />";
    echo $CurrencyConversionResponse->ConversionTime . "<br />";
    echo $CurrencyConversionResponse->ConversionRate . "<br />";
}