drmlxgmqn18198265 2013-03-25 09:22
浏览 32
已采纳

如何使用PHP解析此XML?

I would like to do parse XML for the url: http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0

I am the beginner of PHP and tried the code as below:

$url = "http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0";

$xml = simplexml_load_file($url) or die("feed not loading");

print_r($xml);

var_dump($xml);

I would like to do echo for each attribute e.g. lat, lon, range.. for this XML url.

I found many resource in stackoverflow which the XML is quite standard. I cannot find an example which is used for this format of XML:

Anyone could give me an idea? Thanks.

  • 写回答

3条回答 默认 最新

  • doudang2817 2013-03-25 09:29
    关注
    $url = "http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    $status = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if($status==200){
        $x = new SimpleXMLElement($data,LIBXML_NOCDATA);
        echo 'lat: '.$x->cell['lat'];
        echo 'lon: '.$x->cell['lon'];
        echo 'range: '.$x->cell['range'];
    }
    

    Update: This is an alternative to curl. If you have curl installed, use curl, it is faster.

    $url = "http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0";
    
    $data = file_get_contents($url);
    $x = new SimpleXMLElement($data,LIBXML_NOCDATA);
    $lat = $x->cell['lat'];
    $lng = $x->cell['lon'];
    $range = $x->cell['range'];
    
    echo 'lat: '.$lat.'<br>';
    echo 'lng: '.$lng.'<br>';
    echo 'range: '.$range.'<br>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址