<?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地址以及访客的城市和县。我需要找到这个城市的天气状况。
此处“{$ geoplugin-&gt; city}”正在向我提供城市信息。
答案 0 :(得分:1)
您需要天气API。查看this one,API文档可用here
使用cURL的PHP示例:
<?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"]);
?>
答案 1 :(得分: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;
}
?>