| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the wp-forecast plugin for WordPress |
| 4 |
* |
| 5 |
* Copyright 2018 Hans Matzen (email : webmaster at tuxlog dot de) |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
* |
| 21 |
* @package wp-forecast |
| 22 |
*/ |
| 23 |
|
| 24 |
if ( ! function_exists( 'openuv_get_data' ) ) { |
| 25 |
/** |
| 26 |
* Function to get data from openuv. |
| 27 |
* |
| 28 |
* @param string $apikey The OpenUV API Key. |
| 29 |
* @param string $lat The latitutde. |
| 30 |
* @param string $lon The longitude. |
| 31 |
*/ |
| 32 |
function openuv_get_data( $apikey, $lat, $lon ) { |
| 33 |
// check parms. |
| 34 |
if ( trim( $apikey ) == '' || trim( $lat ) == '' || trim( $lon ) == '' ) { |
| 35 |
return array(); |
| 36 |
} |
| 37 |
|
| 38 |
$url1 = 'https://api.openuv.io/api/v1/uv?lat=' . $lat . '&lng=' . $lon; // '&alt=' + alt + '&ozone=' + ozone + |
| 39 |
$url2 = 'https://api.openuv.io/api/v1/forecast?lat=' . $lat . '&lng=' . $lon; |
| 40 |
|
| 41 |
// Create a stream. |
| 42 |
$opts = array( |
| 43 |
'http' => array( |
| 44 |
'method' => 'GET', |
| 45 |
'header' => "x-access-token: $apikey\r\n", |
| 46 |
), |
| 47 |
); |
| 48 |
|
| 49 |
$context = stream_context_create( $opts ); |
| 50 |
|
| 51 |
$eheaders = array( 'headers' => array( 'x-access-token' => $apikey ) ); |
| 52 |
|
| 53 |
// Open the file using the HTTP headers set above. |
| 54 |
$file1 = wp_remote_get( $url1, $eheaders ); |
| 55 |
if ( is_wp_error( $file1 ) ) { |
| 56 |
return $file1; |
| 57 |
} |
| 58 |
$data = json_decode( $file1['body'], true ); |
| 59 |
|
| 60 |
$file2 = wp_remote_get( $url2, $eheaders ); |
| 61 |
if ( is_wp_error( $file2 ) ) { |
| 62 |
return $file2; |
| 63 |
} |
| 64 |
$data2 = json_decode( $file2['body'], true ); |
| 65 |
|
| 66 |
// add forecast data to array. |
| 67 |
$data['result']['forecast'] = $data2['result']; |
| 68 |
|
| 69 |
// add copyright notice. |
| 70 |
$data['result']['copyright'] = 'UV Data is delivered by openuv.io'; |
| 71 |
|
| 72 |
return $data['result']; |
| 73 |
} |
| 74 |
} |
| 75 |
|