我正在尝试从网址获取xml提要

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

但似乎失败了。我试过了
  • new SimpleXMLElement
  • file_get_contents
  • http_get

  • 但是,当我使用echoprint_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>";
    }
    ?>
    

    关于php - 将XML从URL转换为变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14105922/

    10-12 07:38
    查看更多