【问题标题】:How to get data from an phrase in remote XML in php?如何从 php 中的远程 XML 中的短语中获取数据?
【发布时间】:2020-02-12 03:40:52
【问题描述】:

我想从特定节点的远程站点上的 XML 文件中获取数据。但是我收到以下错误

警告:simplexml_load_file(): 在 php 行中

关于警告 2:它正在加载文件数据。我想要的结果是获得 Grate。

注意:我在我的 php 安装中启用了 SimpleXML 模块。


<?php
  $url = "http://api.srinivasajewellery.com/getrate/getrate";
  $xml = simplexml_load_file($url) or die("not open");

  ?><pre><?php //print_r($xml); ?></pre><?php

  foreach($xml->GRate as $GRate){
    printf('$GRate');
  }
?>

我希望在我的输出中得到“3640.00”,但错误如下

警告:simplexml_load_file():http://api.srinivasajewellery.com/getrate/getrate:1: 解析器错误:需要开始标记,在 H:\root\home\srinivasauser-001\www\goldrate\wp-content\themes\twentynineteen\footer 中找不到“

警告:simplexml_load_file(): {"Grate":"3640.00","SRate":"49.00","PRate":"0.00"} 在 H:\root\home\srinivasauser-001\www\goldrate\ wp-content\themes\twentynineteen\footer.php 在第 24 行

警告:simplexml_load_file(): ^ in H:\root\home\srinivasauser-001\www\goldrate\wp-content\themes\twentynineteen\footer.php 第 24 行 没有打开。

【问题讨论】:

    标签: php xml simplexml


    【解决方案1】:

    当使用默认设置从 PHP 请求 URL“http://api.srinivasajewellery.com/getrate/getrate”时,它将以 JSON 格式返回数据。在这种情况下,可能更容易解析:

    <?php
    
    $url = "http://api.srinivasajewellery.com/getrate/getrate";
    $json = json_decode(file_get_contents($url));
    
    echo '$GRate: ' . $json->GRate, "\n";
    

    输出:

    $GRate: 3670.00
    

    这可以通过获取 URL 并逐字输出来轻松检查:

    $buffer = file_get_contents($url);
    echo $buffer, "\n";
    
    {"GRate":"3670.00","SRate":"50.00","PRate":"0.00"}
    

    就像demonstrated by Vijay Dohare 一样,可以告诉服务器首选XML。要检查它是否有效,也可以通过这种方式:

    stream_context_get_default(['http' => ['header' => 'Accept: application/xml']]);
    $buffer = file_get_contents($url);
    echo $buffer, "\n";
    

    那么输出并没有那么美化(我猜如果数据更多,JSON不会那么容易阅读并且它也会变得更大):

    <GetRateController.Rate xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Savings.Controllers"><GRate>3670.00</GRate><PRate>0.00</PRate><SRate>50.00</SRate></GetRateController.Rate>
    

    这可能与在浏览器中打开 URL 时类似。这是因为浏览器还发送了Accept 请求标头,并且它还包含 XML:

    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
    

    因为浏览器通常也接受 XML(尽管他们更喜欢 HTML)。

    所以最终这取决于你喜欢什么。 JSON which is less verbose compared to XML(参见上面的第一个示例代码)或者如果您想将 XML 与 SimpleXML 一起使用:

    <?php
    
    $url = "http://api.srinivasajewellery.com/getrate/getrate";
    stream_context_get_default(['http' => ['header' => 'Accept: application/xml']]);
    $xml = simplexml_load_file($url) or die("not open");
    
    echo '$GRate: ' . $xml->GRate, "\n";
    

    输出:

    $GRate: 3670.00
    

    【讨论】:

      【解决方案2】:

      试试下面的代码,

      <?php
        $url = "http://api.srinivasajewellery.com/getrate/getrate";
        $context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
      
        $xml = file_get_contents($url, false, $context);
      
        $xml = simplexml_load_string($xml) or die("not open");
      
        foreach($xml->GRate as $GRate){
          echo '$GRate: '.$GRate;
        }
      ?>
      

      【讨论】:

        猜你喜欢
        • 2012-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-19
        • 1970-01-01
        相关资源
        最近更新 更多