Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中从远程XML中的短语中获取数据?_Php_Xml_Simplexml - Fatal编程技术网

如何在php中从远程XML中的短语中获取数据?

如何在php中从远程XML中的短语中获取数据?,php,xml,simplexml,Php,Xml,Simplexml,我想从一个特定节点的远程站点上的XML文件中获取数据。但是我得到了以下错误 警告:simplexml_load_file():在php行中 警告2:正在加载文件数据。结果我想得到格栅 注意:我已经在php安装中启用了SimpleXML模块 <?php $url = "http://api.srinivasajewellery.com/getrate/getrate"; $xml = simplexml_load_file($url) or die("not open");

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

警告:simplexml_load_file():在php行中

警告2:正在加载文件数据。结果我想得到格栅

注意:我已经在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():解析器错误:应为开始标记,“请尝试以下代码

<?php

$url = "http://api.srinivasajewellery.com/getrate/getrate";
$json = json_decode(file_get_contents($url));

echo '$GRate: ' . $json->GRate, "\n";

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

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

{"GRate":"3670.00","SRate":"50.00","PRate":"0.00"}
一直以来,可以告诉服务器XML是首选的。要检查这是否有效,也可以通过以下方式:

<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>
输出并没有那么漂亮(我想如果数据更多,JSON就不会那么容易读取,它也会变得更大):

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

因此,最终这取决于你喜欢什么。或者(请参阅上面的第一个示例代码),或者如果您希望将XML与SimpleXML一起使用:

$GRate: 3670.00
stream_context_get_default(['http' => ['header' => 'Accept: application/xml']]);
$buffer = file_get_contents($url);
echo $buffer, "\n";
<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>
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
<?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