2012-08-25 102 views
5

我使用的代碼根據我插入的Yahoo API URL來更改我的WordPress網站的背景。無論如何,無需任何數據庫就可以自動顯示訪問者的天氣嗎?我雖然使用谷歌的API,但我是編程新手,並不知道如何將其實施到我的網站。請幫忙! 該代碼可以在這裏查看:http://css-tricks.com/using-weather-data-to-change-your-websites-apperance-through-php-and-css/根據用戶位置顯示天氣

請徹底,因爲我是PHP新手 謝謝!

回答

5

這將從Data Science Toolkit使用開源IP2Coordinates service從其IP地址獲得用戶的郵政編碼。

有更準確的地理位置API,但這個是完全免費的,死了很容易使用。如果我在生產網站上開發這個工具,我會使用HTML5 Geolocation作爲我的主要方法,並回退到更好的IP地理定位器,如Max Mind

請注意,這隻適用於您網站的美國用戶。您應該可以將更詳細的數據傳遞給其他雅虎地點API之一以獲取他們的一個WOEID(或選擇不同的天氣API)。

這裏是你的代碼。在示例代碼中的<?php標籤後面加上這個。一旦確定它正在工作,請註釋掉所有以print開頭的行。

//Get the user's IP address 
$user_ip = $_SERVER['REMOTE_ADDR']; 
//The Data Science Toolkit URL 
$url = 'http://www.datasciencetoolkit.org/ip2coordinates/'; 
//Find the user's 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"); 
+0

我需要更換以獲得此代碼的工作(或開箱即用)?對不起,我明天才可以使用。非常感謝代碼,您應該將其發佈到CSS技巧或其他東西;我認爲這是唯一可以做到這一點的教程之一! – user1373771

+0

它應該開箱即用。 – cpilko

+0

它完美的作品!我所要做的只是刪除打印文本的部分(它搞砸了我的wp主題),它像魅力一樣工作。你有一個網站嗎?我很想在頁腳中添加一個功勞! – user1373771

1

使用ipinfo.ioOpenWeatherMap

的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> 

它會顯示類似6.3ºC

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); 
+1

您還可以獲得更多預測 '$ api_3 ='http://api.openweathermap.org/data/2.5/forecast?lat='。 $ point [0]。 '&lon ='。 $點[1]。 '&appid ='。$ API_KEY; $ forecast = file_get_contents($ api_3);' –