2012-08-25 36 views
5

Eklediğim Yahoo API URL'sine göre wordpress'in web sitesi arka planını değiştiren bir kod kullanıyorum. Herhangi bir veritabanını kullanmadan Ziyaretçiler hava durumunu otomatik olarak göstermek için zaten var mı? Google'ın API'sini kullanıyorum ama programlama konusunda bir acemiyim ve web siteme nasıl uygulayacağımı bilmiyorum. Lütfen yardım et! Buradaki kod görüntülenebilir: http://css-tricks.com/using-weather-data-to-change-your-websites-apperance-through-php-and-css/Kullanıcıların konum bilgisine göre hava durumunu göster

Lütfen, PHP için yeni olduğum için dikkatli olunuz.

cevap

5

Bu, kullanıcının posta kodunu, Data Science Toolkit numaralı açık kaynaklı IP2Coordinates service numaralı IP adresini kullanarak IP adreslerinden alır.

Orada daha doğru coğrafi konum API'leri vardır, ancak bu kesinlikle ücretsiz ve kullanımı son derece basittir. Bunu bir üretim sitesinde geliştirmekteydim, HTML5 Geolocation'u, birincil yöntem olarak Max Mind gibi daha iyi bir IP geolocator'a geri dönüş olarak kullanırdım.

Bunun yalnızca sitenizdeki ABD kullanıcıları için çalışacağını unutmayın. Muhtemelen, WATID'lerinden birini kullanmak için (veya farklı bir hava durumu API'sini seçmek için) diğer Yahoo yer API'larından birine daha ayrıntılı veriler vermelisiniz.

İşte size kod. Örnek kodunuzda <?php etiketinden sonra bunu sağa koyun. Çalıştığından emin olduğunuzda, print ile başlayan tüm satırları yorumlayın.

//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)); 

Şimdi buna $data = ile başlayan örnek kod satırı değiştirin:

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

Bu kodu işe yarayacak şekilde değiştirmek için neye ihtiyacım var (veya kutunun dışında çalışır)? Üzgünüm, yarına kadar bir yazılıma erişimim yok. kod için çok teşekkürler ve bunu CSS hilelerine veya bir şeye göndermelisiniz; Bunun, bunu yapabilecek tek öğreticilerden biri olduğunu düşünüyorum! – user1373771

+0

Kutunun dışında çalışmalıdır. – cpilko

+0

Mükemmel çalışıyor! Yapmam gereken tek şey metin yazdığı parçaları silmekti (wp temamı karıştırdı) ve bir çekicilik gibi çalıştı. Web siten var mı? Altbilgide size kredi eklemeyi çok isterim! – user1373771

1

ipinfo.io ve OpenWeatherMap kullanma

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> 

O Konuma (ipapi.co) ve yer (openweathermap.org) hava tahminlerini almak için başka birine haritalama ziyaretçiler için IP adresini bir API gerekir 6,3 ºC

5

gibi bir şey gösterecektir. Aşağıdaki kod, php (sunucu tarafı) içindir:

# 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=' adresinde de genişletilmiş tahmin alabilirsiniz. $ point [0]. '& lon ='. $ point [1]. '& appid ='.$ Api_key; $ forecast = file_get_contents ($ api_3); ' –

İlgili konular