Skip to content

Instantly share code, notes, and snippets.

@anushshukla
Created June 6, 2019 06:36
Show Gist options
  • Select an option

  • Save anushshukla/821268c372016fcc897d8c0b3c962ad9 to your computer and use it in GitHub Desktop.

Select an option

Save anushshukla/821268c372016fcc897d8c0b3c962ad9 to your computer and use it in GitHub Desktop.
PHP file showing real time climate update as per user's location
<?php
// Server: Apache2
// Language: PHP7.0.17-2
// HTML5, CSS3 & JS
define('ENV', 'DEVELOPMENT');
error_reporting(-1);
ini_set('display_errors', ENV !== 'PRODUCTION');
if(request()->isAjax && request()->isPost())
{
$latitude = @$_REQUEST['coords']['latitude'] ?: 19.0760;
$longitude = @$_REQUEST['coords']['longitude']?: 72.8777;
$apiKey = '9a171ea6014ab4a5';
$weatherJsonStr = file_get_contents("http://api.wunderground.com/api/$apiKey/conditions/q/$latitude,$longitude.json");
$weatherJson = json_decode($weatherJsonStr);
$temperature = $weatherJson->current_observation->temp_c;
// $temperature = 18;
$notification = array(
"notify" => true
,"title" => "Weather Reporter!"
,"url" => "http://localhost/task.php"
// ,"data" => $weatherJson
);
switch (true) {
case $temperature > 35:
$notification['desc'] = 'God damn it! This heat must make you take a dive into a chilled pool!';
break;
case $temperature > 25:
$notification['desc'] = 'It\'s getting quite hot! You must turn on the AC to be cool';
break;
case $temperature > 15:
$notification['desc'] = 'This is what we call a pleasant climate, enjoy it till it lasts';
break;
case $temperature > 5:
$notification['desc'] = 'I hope you are carrying your favorite blanket with you as you would need it!';
break;
default:
$notification['desc'] = 'Whoa! Its freezing out there, make sure you have layers of cloths on you!';
break;
}
header("HTTP/1.1 200 Browser Push Ballon Notification!");
header('Content-Type: application/json');
echo json_encode($notification);
die;
}
?>
<!DOCTYPE html>
<html>
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css">
<style type="text/css">
header, footer {position: fixed;left: 0;width: 100%;padding: 20px 0;background: rgba(204, 204, 204, 0.17)}
header {top: 0;border-bottom: 1px solid #ccc;}
footer {bottom: 0;border-top: 1px solid #ccc;}
main {position: relative;top: 61px;display: table;width: 100%;}
h1 {margin: 0;display: table-cell;vertical-align: middle;}
.zoom-text-on-load {
-webkit-animation:bounceIn 2s;
}
</style>
<title>Ultimate | Homepage</title>
</head>
<body class="fluid-container">
<header class="text-center">Weather Forecast</header>
<main>
<h1 class="text-center">
<div class="zoom-text-on-load">Coming Soon</div>
</h1>
</main>
<footer class="text-center">
<small>&copy; Copyright 2017 Ultimate Website</small>
</footer>
<script type="text/javascript"
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script type="text/javascript"
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>
<script type="text/javascript">
function setMainHeight() {
$("main").height($(window).height() - $("header").outerHeight() - $("footer").outerHeight());
}
$(window).resize(function() {
setMainHeight();
});
$(document).ready(function() {
setMainHeight();
});
function doPoll(position) {
// Get the JSON
$.ajax({
method:'post',
data:position,
success: function(data){
if(data.notify) {
// Yeah, there is a new notification! Show it to the user
var notification = new Notification(data.title, {
icon:'https://www.inscripts.com/media/background3.png',
body: data.desc,
});
notification.onclick = function () {
window.open(data.url);
};
}
// Retry after a second
var millisecond = 1000;
var second = 1;
var minute = second * 60;
var day = minute * 24;
// setTimeout(doPoll,day * millisecond);
}, dataType: "json"});
}
if (navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
if(Notification.permission !== "granted") {
Notification.requestPermission().then(function(result) {
if (result === 'default') {
doPoll(position);
}
});
} else {
doPoll(position);
}
});
} else {
BootstrapDialog.alert('You really need to update your browser!');
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment