加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从URL获取xml到变量

发布时间:2020-12-13 13:35:52 所属栏目:PHP教程 来源:网络整理
导读:我想从网址获取xml提要 http://api.eve-central.com/api/marketstat?typeid=1230regionlimit=10000002 但似乎失败了.我试过了 new SimpleXMLElement , file_get_contents 和 http_get 然而,当我回显或print_r时,这些似乎都没有回应一个很好的XML提要.最终目
我想从网址获取xml提要
http://api.eve-central.com/api/marketstat?typeid=1230&regionlimit=10000002

但似乎失败了.我试过了

> new SimpleXMLElement,
> file_get_contents
> http_get

然而,当我回显或print_r时,这些似乎都没有回应一个很好的XML提要.最终目标是最终解析这些数据,但将其变为变量肯定是一个不错的开始.

我在下面附上了我的代码.这包含在一个循环中,$typeID确实提供了正确的ID,如上所示

$url = 'http://api.eve-central.com/api/marketstat?typeid='.$typeID.'&regionlimit=10000002';
echo $url."<br />";
$xml = new SimpleXMLElement($url);
print_r($xml);

我应该说,我看到的另一个奇怪的事情是,当我回复$url时,我得到了

http://api.eve-central.com/api/marketstat?typeid=1230?ionlimit=10000002

& reg是注册商标符号.我不确定这是我浏览器中的“功能”,还是我代码中的“功能”

请尝试以下方法:
<?php
$typeID = 1230;
// set feed URL
$url = 'http://api.eve-central.com/api/marketstat?typeid='.$typeID.'&regionlimit=10000002';
echo $url."<br />";
// read feed into SimpleXML object
$sxml = simplexml_load_file($url);

// then you can do
var_dump($sxml);

// And now you'll be able to call `$sxml->marketstat->type->buy->volume` as well as other properties.
echo $sxml->marketstat->type->buy->volume;

// And if you want to fetch multiple IDs:
foreach($sxml->marketstat->type as $type){
    echo $type->buy->volume . "<br>";
}
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读