Show weather based on users location

I am using code that changes my background on WordPress according to the Yahoo API URL that I inserted. Is there a way to get it to automatically show visitors weather without any databases? I use the Google API, but I am new to programming and do not know how to implement it on my website. Please help! The code can be seen here: http://css-tricks.com/using-weather-data-to-change-your-websites-apperance-through-php-and-css/

Please be careful because I'm new to PHP Thank you!

+5
source share
3 answers

IP- IP2Coordinates Data Science Toolkit.

API-, , . , HTML5 Geolocation IP, Max Mind.

, . , API- Yahoo, WOEID ( API ).

. <?php . , , , print.

//Get the user IP address
$user_ip = $_SERVER['REMOTE_ADDR'];
//The Data Science Toolkit URL
$url = 'http://www.datasciencetoolkit.org/ip2coordinates/';
//Find the user location from their IP. 
//*** You need the get_data function from the sample code
$raw_geocode = json_decode( get_data( $url . $user_ip) );
//Check if the user is in the US
if ('US' === $raw_geocode->$user_ip->country_code) {
  //If yes, store their zip code in a variable, and print it
  $zip_code = $raw_geocode->$user_ip->postal_code;
  printf('<p>Your zip code is: %s</p>', $raw_geocode->$user_ip->postal_code);
} else {
  //If the user isn't in the US, set a sip code that will work.
  $zip_code = '97211';
  //and print an error
  printf('<p>Sorry, this app does not work in %s.</p>', $raw_geocode->$user_ip->country_name);
}

//Print the raw data for debugging.
printf('<pre>%s</pre>', print_r($raw_geocode, true));

, $data =, :

$data = get_data("http://weather.yahooapis.com/forecastrss?p={$zip_code}&u=f");
+5

API IP- (ipapi.co), - (openweathermap.org). php ( ):

# Part 1 (get latitude & longitude)
$ip = '1.2.3.4';
$api_1 = 'https://ipapi.co/' . $ip . '/latlong/';
$location = file_get_contents($api_1);
$point = explode(",", $location);

# Part 2 (get weather forecast)
$api_2 = 'http://api.openweathermap.org/data/2.5/weather?lat=' . $point[0] . '&lon=' . $point[1] . '&appid=API_KEY';
$weather = file_get_contents($api_2);
+5

Using ipinfo.io and OpenWeatherMap

JavaScript:

<script type="text/javascript" src="jquery.simpleopenweather.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $.get("http://ipinfo.io", function (response) {
            $("#weather").attr('data-simpleopenweather-city', response.city).simpleopenweather({template: '<h2>'+'{{temperature}} '+'&deg;'+'C'+'<h2>', units: 'metric'});
        }, "jsonp");
    });
</script>

HTML:

<div id="weather" class="simpleopenweather" data-simpleopenweather-city=""></div>

It will show something like 6.3 ºC

+1
source

All Articles