-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathproxy.php
24 lines (17 loc) · 930 Bytes
/
proxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
// File Name: proxy.php
function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
$api_key = 'YOUR_API_KEY';
$api_endpoint = 'https://api.darksky.net/forecast';
$lat = ( isset( $_GET[ 'lat' ] ) && is_numeric( $_GET[ 'lat' ] ) ? ( float )( $_GET[ 'lat' ] ) : null );
$lon = ( isset( $_GET[ 'lon' ] ) && is_numeric( $_GET[ 'lon' ] ) ? ( float )( $_GET[ 'lon' ] ) : null );
$units = ( isset( $_GET[ 'units' ] ) && strtolower( $_GET[ 'units' ] ) == 'si' ? 'si' : 'us' );
$api_url = $api_endpoint . '/' . $api_key . '/' . $lat . ',' . $lon . '/?units=' . $units . '&exclude=minutely,hourly,alerts,flags';
if( ! isset ( $api_url ) ) { die( 'no api URL found' ); }
if( get_http_response_code( $api_url ) !== '200' ){ die( 'URL format invalid' ); }
$api_data = file_get_contents( $api_url );
header('Content-type:application/json;charset=utf-8');
echo ( $api_data ) ;