MET Office getting data using the new DataHub [closed]

2 weeks ago 16
ARTICLE AD BOX

I am trying to get some data from the MET Office DataHub using the code below but all I get is the following error: If anyone has the time could you please have a look at my code and try see where I have gone wrong

{"code":"404","type":"Status report","message":"Not Found","description":"The requested resource is not available."}

My script:

<?php date_default_timezone_set('UTC'); // HARD-CODED API KEY $API_KEY = 'Private Key'; // Base API URL $BASE_URL = 'https://data.hub.api.metoffice.gov.uk/atmospheric-models/1.0.0'; // Log file $LOG_FILE = dirname(__FILE__) . '/ss_download.log'; function write_log($level, $message) { global $LOG_FILE; $entry = date('Y-m-d H:i:s') . ' - ' . $level . ' - ' . $message . "\n"; file_put_contents($LOG_FILE, $entry, FILE_APPEND); } $timesteps = isset($_GET['timesteps']) ? $_GET['timesteps'] : 'hourly'; $latitude = isset($_GET['latitude']) ? $_GET['latitude'] : '51.4680'; $longitude = isset($_GET['longitude']) ? $_GET['longitude'] : '0.4550'; $excludeMetadata = isset($_GET['excludeMetadata']) ? $_GET['excludeMetadata'] : 'TRUE'; $includeLocation = isset($_GET['includeLocation']) ? $_GET['includeLocation'] : 'TRUE'; if ($latitude === '' || $longitude === '') { http_response_code(400); echo 'ERROR: Latitude and longitude must be supplied.'; exit; } if ($timesteps !== 'hourly' && $timesteps !== 'three-hourly' && $timesteps !== 'daily') { http_response_code(400); echo 'ERROR: Timesteps must be hourly, three-hourly or daily.'; exit; } $url = $BASE_URL . $timesteps; $params = array( 'excludeParameterMetadata' => (strtoupper($excludeMetadata) === 'TRUE') ? 'true' : 'false', 'includeLocationName' => (strtoupper($includeLocation) === 'TRUE') ? 'true' : 'false', 'latitude' => $latitude, 'longitude' => $longitude ); $queryString = http_build_query($params); $headers = array( 'Accept: application/json', 'apikey: ' . $API_KEY ); $retries = 5; $success = false; while (!$success && $retries > 0) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url . '?' . $queryString); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch); $error = curl_error($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($error) { write_log('WARNING', 'cURL error: ' . $error); $retries--; sleep(10); } else { $success = true; } if ($retries === 0) { write_log('ERROR', 'Retries exceeded'); http_response_code(500); echo 'ERROR: Unable to retrieve forecast.'; exit; } } header('Content-Type: application/json; charset=utf-8'); $data = json_decode($response, true); if (isset($data['code']) && $data['code'] === '900908') { // Friendly message for forbidden resource http_response_code(403); echo json_encode(array( 'error' => 'API_KEY_FORBIDDEN', 'message' => 'Your API key is not authorized to access this resource. Check the key, endpoint permissions, and IP restrictions in your Met Office account.' )); exit; } http_response_code($status); echo $response;

I unsuccessfully asked for help from the MET Office support but they redirected me to the Stack.

Read Entire Article