Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
游客天气信息的PHP代码_Php_Weather - Fatal编程技术网

游客天气信息的PHP代码

游客天气信息的PHP代码,php,weather,Php,Weather,您需要一个天气API。看看,API文档是可用的 使用cURL的PHP示例: <?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->l

您需要一个天气API。看看,API文档是可用的

使用cURL的PHP示例:

<?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>"; 
?>

以下是答案

<?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"]);
?>

你在找气象服务吗?这可能会帮助你,我已经有一个关键,我需要把在网址。世界天气在线。com/free/v1/weather.ashx?q=“.$City.”&format=json&num_of_days=5&key=“$APIKey@Arun您的评论很难理解,但我认为您的意思是“我把API密钥放在哪里”-在我给您的示例中,替换变量“$APIKey”“使用您的API密钥。谢谢thomas u完美地回答了我的问题。@Arun请看如何接受答案,但是thomas我得到的结果很难理解。有没有办法单独测量温度?
<?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;
}
?>