1

I'm somewhat familliar with xml and json parsing but Im having a problem with this site. I tried

$json_string="http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml"; 
$json = file_get_contents($json_string); 
$arr=json_decode($json_string, true);

but it doesn't work. When i was analysing the data i found that there are some javascript variables inside json that are turned to data when javascript is run so maybe thats why it doesn't work. Not shure how to fix it thou.

What I would like to do is parse values like "9:10 CEST"... and "si0_20140930-0710_zm_si.jpg"... into php array.

4
  • 1
    It's XML and not JSON you're trying to decode. Use simplexml instead. Commented Sep 30, 2014 at 10:39
  • file is xml. Then, why you are using json_decode() ? Commented Sep 30, 2014 at 10:39
  • doesnt work with xml eather Commented Sep 30, 2014 at 10:43
  • The data format on that file seems weird. It's neither XML nor JSON. No wonder the language facility isn't working. I guess you'll have to write some custom logic. Commented Sep 30, 2014 at 11:17

3 Answers 3

1

The following solution works. Don't know if there's a better way to do it:

<?php
    $xml_string="http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml"; 
    $xml = file_get_contents($xml_string);

    // Extract relevant section out of the file
    $start_pos = strpos($xml, "timeline:") + strlen("timeline:");
    $end_pos = strpos($xml, "});");
    $json = substr($xml, $start_pos, $end_pos - $start_pos);

    // Some string replace operations to bind the keys and values within " (double quotes) 
    $json = preg_replace("/(,[a-z]+)/", '"$1', $json);
    $json = preg_replace("/([a-z]+)(:)/", '"$1"$2"', $json);
    $json = str_replace('}', '"}', $json);

    // echo $json; // This string is now in decodable json format
    $arr = json_decode($json, true);
    var_dump($arr);
    return;
?>
Sign up to request clarification or add additional context in comments.

1 Comment

This is what i was looking for...I modified it a bit and now is perfect ! THX.
0

use :

$xml=simplexml_load_file("http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml");

do not use json_decode.

1 Comment

I tried $url= file_get_contents("meteo.arso.gov.si/uploads/probase/www/plus/timeline/…); $xml = simplexml_load_string($url); print_r($xml); but it doesnt work. It gives me SimpleXMLElement Object ( )
0

This xml doesn't seem a valid one. It doesn't contain any DTD for the data rules or even the standard <?xml.. tags, like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
...

It contains also javascript code. SimpleXML cannot parse it either (empty array).

If you can call the javascript that it holds, do this:

$data = file_get_contents("http://meteo.arso.gov.si/uploads/probase/www/plus/timeline/timeline_radar_si_short.xml");
$data = str_replace(array('<?xml version="1.0" encoding="utf-8"?><pujs><![CDATA[',']]></pujs>'),"",$data);
echo '<script type="text/javascript">'.$data."</script>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.