dt250827 2017-04-06 15:01
浏览 108
已采纳

Simplexml_load_file正在使用一个示例,但不与其他示例一起使用

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 = 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:

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:

$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:

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

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

  • 写回答

1条回答 默认 最新

  • doumi9661 2017-04-06 15:28
    关注

    Error 1 (invalid xml):

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

    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.

    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 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:

    $rate = (string)$xml->ExchangeRatesTable->Rates->Rate[$i]->Mid
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥30 itest不允许查看成绩怎么办
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏