0

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!

Community
  • 1
  • 1
user30058
  • 167
  • 1
  • 8

1 Answers1

1

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];
  • Thanks for the reply. I tried both. Apparantly when I use your first option it gives me only this response: SimpleXMLElement Object ( ) . The second answer is giving me a blank page. Am I doing something wrong? I use Aptana with Xampp apache running. I am completely new to php. – user30058 Mar 17 '16 at 13:06
  • Yes the first output is correct, please use the last code in my answer to get the line. (`$coord = $xm .... `) – KIMB-technologies Mar 17 '16 at 13:09
  • Yes! This works, thank you very much. Could you only explain why a prin $xml doesnt show the whole xml but only an empty page with SimpleXMLElement Object ( )? – user30058 Mar 17 '16 at 13:18
  • With `simplexml_load_string($data);` you are creating an XML object and you can not display all objects directly. You have to use a method to do something with it's data. (In this case `SimpleXMLElement::xpath();` follows the path to the coordinates and outputs them.) Please read about PHP ObjectOrientedProgramming. – KIMB-technologies Mar 17 '16 at 13:21
  • Will do. Much appreciated for the help. – user30058 Mar 17 '16 at 13:28