added Google GeoCoding API implementation and cURL fetch routine

This commit is contained in:
Harald Milz 2019-04-09 14:54:04 +02:00
parent cdf1f988c1
commit 34d6ee673d
3 changed files with 39 additions and 26 deletions

View File

@ -128,7 +128,7 @@ function gcal_geocoding_setting_string() {
),
array(
'option' => 'official',
'name' => 'Google official - in Entwicklung; erfordert einen API Key --> ',
'name' => 'Google official - erfordert einen API Key --> ',
),
array(
'option' => 'inofficial',
@ -144,7 +144,7 @@ function gcal_geocoding_setting_string() {
$checked = ( $current == $coder['option'] ? 'checked' : '' );
echo '<input type="radio" id="gcal_geocoding" name="gcal_options[gcal_geocoding]" value ="' . $coder['option'] . '" ' . $checked . '> ' . $coder['name'];
if ( $coder['option'] == 'official' ) {
echo '<input type="text" size="32" id="gcal_geocoding" name="gcal_options[gcal_apikey]" value="' . $apikey . '">';
echo '<input type="text" size="48" id="gcal_geocoding" name="gcal_options[gcal_apikey]" value="' . $apikey . '">';
}
echo '</br>' ;

View File

@ -136,19 +136,22 @@ Suchen: if ( isset ( $options['geocache']['hashx'] ) ) ...
function gcal_import_geocode_official($location) {
$options = get_option('gcal_options');
$apikey = $options['apikey'];
if ( empty ($apikey) ) { // ??? we should handle this in the admin frontend.
if ( ! isset ( $options['gcal_apikey'] ) || '' == $options['gcal_apikey'] ) { // ??? we should handle this in the admin frontend.
gcal_error_log ("WARN: using Google official geocoding but provided no APIKEY");
return array ('','');
} else {
$apikey = $options['gcal_apikey'];
$location = urlencode($location);
$useragent = 'Mozilla/5.0 (X11; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0';
// https://developers.google.com/maps/documentation/geocoding/start
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$location&key=$apikey";
$response = wp_remote_get($url);
$json = wp_remote_retrieve_body($response);
$http_code = wp_remote_retrieve_response_code($response);
// we need to catch errors, e.g. wrong APIKEY, like this:
$response = curl_get_remote($url);
$decoded = json_decode($response, true);
$lat = $decoded['results']['0']['geometry']['location']['lat'];
$lon = $decoded['results']['0']['geometry']['location']['lng'];
gcal_error_log ("INFO: " . __FUNCTION__ . " found lat $lat lon $lon");
return array ($lat, $lon);
/*
{
"error_message" : "The provided API key is invalid.",
@ -156,13 +159,6 @@ function gcal_import_geocode_official($location) {
"status" : "REQUEST_DENIED"
}
*/
// https://www.php.net/manual/en/function.json-decode.php
$decoded = json_decode($json, true);
$file = dirname (__FILE__) . '/json-decoded.txt';
// should be results->geometry->location->lat/lng.
file_put_contents ($file, var_export ($decoded, TRUE));
return array ('','');
}
}

View File

@ -7,7 +7,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
// we'll set this as category => proxy, link => link, active => 0;
// in the admin page, all entries will be displayed with a checkbox for activating, deactivating, deleting.
require_once dirname ( __FILE__ ) . "/gcal-import-geocode.php";
require_once __DIR__ . "/gcal-import-geocode.php";
/**
@ -100,16 +100,36 @@ The update and delete logic goes as follows:
add_action( 'gcal_import_worker_hook', 'gcal_import_worker' );
require_once dirname (__FILE__) . '/icalparser/src/IcalParser.php';
require_once dirname (__FILE__) . '/icalparser/src/Recurrence.php';
require_once dirname (__FILE__) . '/icalparser/src/Freq.php';
require_once dirname (__FILE__) . '/icalparser/src/WindowsTimezones.php';
require_once __DIR__ . '/icalparser/src/IcalParser.php';
require_once __DIR__ . '/icalparser/src/Recurrence.php';
require_once __DIR__ . '/icalparser/src/Freq.php';
require_once __DIR__ . '/icalparser/src/WindowsTimezones.php';
function curl_get_remote($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
if ( curl_errno($ch) ) {
// $info = curl_getinfo($ch);
$message = __FUNCTION__ . ": cURL error " . curl_error($ch);
// gcal_error_log ("WARN: $message");
curl_close($ch);
throw new \RuntimeException($message);
}
curl_close($ch);
// now we're sure we have a valid response:
return ($response);
}
function gcal_import_do_import($category, $link) {
$my_latlon = array('', '');
$cal = new \om\IcalParser();
$results = $cal->parseFile($link);
$results = $cal->parseString(curl_get_remote($link));
// TODO: Fehlerbehandlung, wenn der Link kaputt ist. Muss graceful passieren.
// icalparser nutzt intern file_get_contents, und da kommt man nicht ohne weiteres ran. Evtl. ändern auf curl?
@ -286,13 +306,10 @@ function gcal_import_do_import($category, $link) {
// and update the _created field
update_post_meta ( $id, '_gcal_created', $lastmodified );
gcal_error_log ("INFO: updated post $post_id");
} elseif ( $lastmodified == $created ) {
// it was not modified after we created it, so we only update the recent tag.
} elseif ( $lastmodified < $created ) {
// iiiiek! A time reversal or a secret time machine! That should not happen!
gcal_error_log ("WARN: post $id last-modified : created $lastmodified < $created ");
}
// and set the event to recent = true no matter what.
} // else both are equal, and we do nothing except setting recent to true.
update_post_meta ( $id, '_gcal_recent', 'true' );
}
} else {