drlndkhib08556095 2016-03-17 12:32
浏览 64

解析php中XML URL的单行

I have a xml URL where I want to parse a single line. But my script cant even read the XML url. I looked at the answer given at PHP parsing XML from URL but I still get 3 errors at the file_get_contents() command. There is no error with some other sites.

my script is:

    <?php
    header('Content-type: text/html; charset=utf-8');

    $url = "https://geodata.nationaalgeoregister.nl/geocoder/Geocoder?zoekterm=1056SW+11";
    $data = file_get_contents($url);
    $data = iconv(mb_detect_encoding($data, mb_detect_order(), true), "UTF-8", $data);
    $xml = simplexml_load_string($data);

    print_r($xml);      
?>

I try to print the whole XML in this case, but eventually i want it to only print line 5 of the xml, the coordinates. How can I overcome the errors, and how can I make sure it only prints one line in this XML?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douchen7366 2016-03-17 12:51
    关注

    Maybe PHP has a problem with the SSL connection (I have PHP 7 and everything works fine).

    On one hand you can try to request the URL without SSL:

    <?php
        header('Content-type: text/html; charset=utf-8');
    
        $url = "http://geodata.nationaalgeoregister.nl/geocoder/Geocoder?zoekterm=1056SW+11";
        $data = file_get_contents($url);
        $data = iconv(mb_detect_encoding($data, mb_detect_order(), true), "UTF-8", $data);
        $xml = simplexml_load_string($data);
    
        print_r($xml);      
    ?>
    

    Or you can use cURL for the connection (prefered method):

     $url = "https://geodata.nationaalgeoregister.nl/geocoder/Geocoder?zoekterm=1056SW+11";
     $c = curl_init();
     curl_setopt($c, CURLOPT_URL, $url);
     curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($c, CURLOPT_TIMEOUT, 10);
     $data = curl_exec($c);
     curl_close($c);
    
     print_r( $data );
    

    And if you have the XML string, you can get the coordinates this way:

    $xml = simplexml_load_string($data);
    
    $coord = $xml->xpath('/xls:GeocodeResponse/xls:GeocodeResponseList/xls:GeocodedAddress/gml:Point/gml:pos');    
    echo $coord[0][0];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题