Simplexml_load_file只处理一个示例,但与另一个示例无关。

时间:2021-06-15 00:12:55

I made a simple website to show actually currency rates from bank website, here is xml from I got these data: http://www.nbp.pl/kursy/xml/LastA.xml. I put this into a code like that:

我制作了一个简单的网站来展示银行网站的汇率,这里是我得到的xml数据:http://www.nbp.pl/kursy/xml/LastA.xml。我把它写成这样的代码:

$xml = simplexml_load_file('http://www.nbp.pl/kursy/xml/LastA.xml');

and get it to variables like that:

把它变成这样的变量:

<?php
  for($i=0; $i<35; $i++){
        $rate = (string)$xml->pozycja[$i]->kurs_sredni;
        $name = (string)$xml->pozycja[$i]->nazwa_waluty;
        $code = (string)$xml->pozycja[$i]->kod_waluty;
      (echo here)

It works. But if I'd like to get this data from this xml, for example from yesterday: http://api.nbp.pl/api/exchangerates/tables/a/2017-04-05?format=xml, and do the same I have only errors:

它的工作原理。但是,如果我想从这个xml中获取这些数据,例如:http://api.nbp.pl/api/exchangerates//a/2017-04-05?格式=xml,并执行相同的错误:

Warning: simplexml_load_file(): http://api.nbp.pl/api/exchangerates/tables/a/2017-04-05:1: parser error : Start tag expected, '<' not found in [PATH] on line 13

Line 13:

13号线:

$xml = simplexml_load_file('http://api.nbp.pl/api/exchangerates/tables/a/2017-04-05');

And other errors:

和其他错误:

Warning: simplexml_load_file(): [{"table":"A","no":"067/A/NBP/2017","effectiveDate":"2017-04-05","rates":[{"curr in [PATH] on line 13

Warning: simplexml_load_file(): ^ in [PATH] on line 13

Notice: Trying to get property of non-object in [PATH] on line 18

Line 18:

18行:

$rate = (string)$xml->Rate[$i]->Mid;

What is difference between these xml's? What am I doing wrong? Could You help me?

这些xml的区别是什么?我做错了什么?你能帮我吗?

1 个解决方案

#1


0  

Error 1 (invalid xml):

You're missing the ?format=xml at the end of the URL.

在URL的末尾缺少了格式=xml。

If you call http://api.nbp.pl/api/exchangerates/tables/a/2017-04-05 (without the format param) through a browser, it seems to default to XML automatically. If you call it through a script, it seems to default to JSON instead.

如果你调用http://api.nbp。pl/api/exchange erates/tables/a/2017-04-05(没有格式参数)通过浏览器,它似乎自动默认为XML。如果您通过一个脚本调用它,它似乎默认为JSON。

So it should work, just by changing

所以它应该是可以改变的。

$xml = simplexml_load_file('http://api.nbp.pl/api/exchangerates/tables/a/2017-04-05');

to

$xml = simplexml_load_file('http://api.nbp.pl/api/exchangerates/tables/a/2017-04-05?format=xml');

Error 2 (property from non-object):

The xml looks like this:

xml是这样的:

<?xml version="1.0" encoding="utf-8"?>
    <ArrayOfExchangeRatesTable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ExchangeRatesTable>
            <Table>A</Table>
            <No>067/A/NBP/2017</No>
            <EffectiveDate>2017-04-05</EffectiveDate>
            <Rates>
                <Rate>
                    <Currency>bat (Tajlandia)</Currency>
                    <Code>THB</Code>
                    <Mid>0.1151</Mid>
                </Rate>
                <Rate>
                    ....

When you're trying to get the Rate, you're missing the first level node (ExchangeRatesTable).
Add it and it will work:

当您试图获取速率时,您将丢失第一个级别的节点(ExchangeRatesTable)。加上它,它就会起作用:

$rate = (string)$xml->ExchangeRatesTable->Rates->Rate[$i]->Mid

#1


0  

Error 1 (invalid xml):

You're missing the ?format=xml at the end of the URL.

在URL的末尾缺少了格式=xml。

If you call http://api.nbp.pl/api/exchangerates/tables/a/2017-04-05 (without the format param) through a browser, it seems to default to XML automatically. If you call it through a script, it seems to default to JSON instead.

如果你调用http://api.nbp。pl/api/exchange erates/tables/a/2017-04-05(没有格式参数)通过浏览器,它似乎自动默认为XML。如果您通过一个脚本调用它,它似乎默认为JSON。

So it should work, just by changing

所以它应该是可以改变的。

$xml = simplexml_load_file('http://api.nbp.pl/api/exchangerates/tables/a/2017-04-05');

to

$xml = simplexml_load_file('http://api.nbp.pl/api/exchangerates/tables/a/2017-04-05?format=xml');

Error 2 (property from non-object):

The xml looks like this:

xml是这样的:

<?xml version="1.0" encoding="utf-8"?>
    <ArrayOfExchangeRatesTable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ExchangeRatesTable>
            <Table>A</Table>
            <No>067/A/NBP/2017</No>
            <EffectiveDate>2017-04-05</EffectiveDate>
            <Rates>
                <Rate>
                    <Currency>bat (Tajlandia)</Currency>
                    <Code>THB</Code>
                    <Mid>0.1151</Mid>
                </Rate>
                <Rate>
                    ....

When you're trying to get the Rate, you're missing the first level node (ExchangeRatesTable).
Add it and it will work:

当您试图获取速率时,您将丢失第一个级别的节点(ExchangeRatesTable)。加上它,它就会起作用:

$rate = (string)$xml->ExchangeRatesTable->Rates->Rate[$i]->Mid