elin-r
10/4/2016 - 10:35 AM

Spara URL (json) som fil med tidsintervall och visa ut

Spara URL (json) som fil med tidsintervall och visa ut

<?php 
$outfile= 'result.json';
$url='http://online.equipe.com/api/v1/class_sections/22570/results.json';
$json = file_get_contents($url);
if (file_exists($outfile) && (filemtime($outfile) > (time() - 60 * 5 ))) {
   // Cache file is less than five minutes old. 
   // Don't bother refreshing, just use the file as-is.
   $json = file_get_contents($url);
   echo "Using cached file!";
} else {
if($json) { 
    if(file_put_contents($outfile, $json)) {
      echo "Saved JSON fetched from “{$url}” as “{$outfile}”.";
    }
    else {
      echo "Unable to save JSON to “{$outfile}”.";
    }
}
else {
   echo "Unable to fetch JSON from “{$url}”.";
}
}
?>

<table id="myTable">
    <tr>
        <td><strong>ID</strong></td>
        <td><strong>Ryttare</strong></td>
        <td><strong>Hästkrake</strong></td>
    </tr>
</table>

<script type="text/javascript">
jQuery(function(){
    jQuery.getJSON("../result.json", function(result){
    // Gets file and lets me use the content of the jsonArray
        for (var i = 0; i < result.length; i++) // Loop through the jsonArray
      {
       jQuery("#myTable").append("<tr>" + 
       "<td>" + result[i].rider_id + "</td>" +
       "<td>" + result[i].rider_name + "</td>" +
       "<td>" + result[i].horse_name + "</td>" 
       + "</tr>");

      }
    });
});
</script>