<?php
function curldata() { 
$apiKey = 'je API key'; // your own API key
$endpointfiles = 'https://api.dataplatform.knmi.nl/open-data/v1/datasets/short_term_weather_forecast/versions/1.0/files?orderBy=created&sorting=desc';


global  $response,$info,$err,$file,$endpointurl;

$curl = curl_init();
curl_setopt_array($curl, [
  CURLOPT_URL => $endpointfiles,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_FOLLOWLOCATION => true,
  //CURLOPT_POST => true,
  //CURLOPT_POSTFIELDS => $post,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20120424 Firefox/12.0",
  CURLOPT_HTTPHEADER => ["Authorization: $apiKey","Content-Type:application/json"],
]);
	$response = curl_exec($curl);
	$info	  = curl_getinfo($curl);
	$err      = curl_error($curl);
    curl_close($curl);

} 

curldata ();



if ($err) {
  echo "cURL Error #:" . $err;
} else { 
  $res = json_decode($response, true);
  $file= $res['files'][1]['filename'];
  
}
$endpoint_url = "https://api.dataplatform.knmi.nl/open-data/v1/datasets/short_term_weather_forecast/versions/1.0/files/". $file."/url";


function curlFileUrl() { 
$apiKey = 'je API key'; // your own API key
$endpointfiles = 'https://api.dataplatform.knmi.nl/open-data/v1/datasets/short_term_weather_forecast/versions/1.0/files?orderBy=created&sorting=desc';


global  $response,$info,$err,$file,$endpoint_url,$response_url;

$curl = curl_init();
curl_setopt_array($curl, [
  CURLOPT_URL => $endpoint_url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_FOLLOWLOCATION => true,
  //CURLOPT_POST => true,
  //CURLOPT_POSTFIELDS => $post,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20120424 Firefox/12.0",
  CURLOPT_HTTPHEADER => ["Authorization: $apiKey","Content-Type:application/json"],
]);
	$response_url = curl_exec($curl);
	$info	  = curl_getinfo($curl);
	$err      = curl_error($curl);
    curl_close($curl);

} 

curlFileUrl();

$url = json_decode($response_url, true);

$test = $url['temporaryDownloadUrl'];

$file_name = "../cache/korte_termijn.html"; // opslaglocatie van je bestand , relatief pad

file_put_contents($file_name, file_get_contents($test));

?>