【php源码】天气预报查询demo
<?php
$city = isset($_GET['city']) ? $_GET['city'] : "广州";
$res = getWeather($city);
$ltd = [
'province' => '省',
'city' => '市',
'time' => '时间',
'temperature' => '温度',
'direction' => '风向',
'now' => '实时',
'ultraviolet' => '提示',
];
echo "<table border='1px solid red' width='600px' align='center'>";
echo "<caption><h1>天气查询</h1></caption>";
if ($res->code == 200) {
foreach ($res->data as $key => $value) {
echo "<tr align='center'><td width='100px' >{$ltd[$key]}</td><td>{$value}</td></tr>";
}
}
echo "<form action='' method='get'><tr align='center'><td>查询</td><td>";
echo "<input type='text' name='city' value={$city}><input type='submit' value='查询'></td></form>";
echo "</table>";
function getWeather($city)
{
$url = "https://www.leybc.com/tools/weather.php?city=" . $city;
header("Content-type:text/html;Charset=utf8");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$content = curl_exec($ch);
curl_close($ch);
return json_decode($content);
}
感谢大佬,互相学习
页:
[1]