2013-08-23 6 views
-3
<?php 
require_once('geoplugin.class.php'); 
$geoplugin = new geoPlugin(); 
// If we wanted to change the base currency, we would uncomment the following line 
// $geoplugin->currency = 'EUR'; 

$geoplugin->locate(); 
echo "<p </span></p> </br>"; 
echo "<p style='text-align: center;'><span style='font-size: 50px; font-family: georgia,palatino; color: #202020 ;'> Your IP Address is {$geoplugin->ip} </span></p> </br>"; 
echo "<p style='text-align: center;'><span style='font-size: 16px; font-weight: bold; text-decoration: underline; font-family: georgia,palatino; color: #D67900;'> More Information About You </span></p>"; 
echo "<p style='text-align: center;'><span style='font-size: 13px; font-family: georgia,palatino; color: #686868;'> Your Country: {$geoplugin->countryName} | City: {$geoplugin->city} | Country Code: {$geoplugin->countryCode} | Longitudes: {$geoplugin->longitude} | Latitude: {$geoplugin->latitude} | Currency Code: {$geoplugin->currencyCode} | Currency Symbol: {$geoplugin->currencySymbol} </span>"; 
?> 

У меня есть код выше, который даст мне IP-адрес вместе с городом и округом посетителя. Мне нужно найти погодное условие города с этим.PHP код Информация о погоде для посетителей

Здесь «{$ gepugin-> city}» дает мне информацию о городе.

+0

Вы ищете метеослужбы? Это может помочь вам http://stackoverflow.com/questions/1305127/free-weather-api –

ответ

1

Вам нужен API погоды. Проверьте this one, Документы API доступны here

PHP-пример использования CURL:

<?php 
    $City = "London"; //Assign the city 
    $APIKey = "42esgfa4pfqp6zwgr4egjbph"; //Assign the API key 
     $WeatherRequest = curl_init(); //make a new cURL request 
     curl_setopt_array($WeatherRequest, array(
      CURLOPT_RETURNTRANSFER => 1, 
      CURLOPT_URL => "http://api.worldweatheronline.com/free/v1/weather.ashx?q=".$City."&format=json&num_of_days=5&key=".$ 
     )); 
     $Response = curl_exec($WeatherRequest); // execute the cURL request and get the reponse 
     curl_close($WeatherRequest); // Close the request after we have received the response 
     $JSON = json_decode($Response, true); 
     //if using Celsius: 
     echo($JSON["data"]["current_condition"]["temp_C"]); 
     //if using fahrenheit: 
     //echo($JSON["data"]["current_condition"]["temp_F"]); 
?> 
+0

У меня уже есть ключ, где мне нужно указать URL http: // api. WorldWeatherOnline. com/free/v1/weather.ashx? q = ". $ City." & format = json & num_of_days = 5 & key = ". $ APIKey – Arun

+0

@Arun ваш комментарий едва понятен, но я думаю, вы имеете в виду« где я могу поместить свой ключ API »- В примере, который я вам дал, замените переменную« $ APIKey »на ваш ключ API. –

+0

спасибо, что thomas u ответил на мой вопрос отлично. – Arun

0

Вот ответ,

<?php 
if (!defined('LOADED')) 
    die('You cannot access this file directly!'); 
require_once('geoplugin.class.php'); 
$geoplugin = new geoPlugin(); 
// If we wanted to change the base currency, we would uncomment the following line 
// $geoplugin->currency = 'EUR'; 

$geoplugin->locate(); 
// XML File 
$feed_url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=" . $geoplugin->city . "&format=xml&extra=localObsTime&num_of_days=5&key=42esgfa4pfqp6zwgr4egjbph"; 

// INITIATE CURL. 
$curl = curl_init(); 

// CURL SETTINGS. 
curl_setopt($curl, CURLOPT_URL, "$feed_url"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); 

// GRAB THE XML FILE. 
$xxml = curl_exec($curl); 

curl_close($curl); 

$xml = new SimpleXMLElement($xxml); 

$flag = 0; 
// Loop through ... only pick the first one 
foreach ($xml->current_condition as $item) { 
    if ($flag == 0) { 
     echo "<p style='text-align: right;'><span style='font-size: 18px; font-family: georgia,palatino; color: #D8D8D8;'> {$geoplugin->city} Weather: {$item->temp_C} C</span>"; 
     echo "<p style='text-align: right;'><span style='font-size: 18px; font-family: georgia,palatino; color: #D8D8D8;'> {$item->localObsDateTime} </span>"; 
    } 
    $flag = 1; 
} 
?>