/
live.php
43 lines (40 loc) · 1.2 KB
/
live.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
if (!isset($_GET['cid']) || strlen($_GET['cid'])!=9 || !ctype_digit($_GET['cid'])) {
$city = '';
$body = '<h3>城市代码不合法!</h3>';
}
else {
$live = getlivedata($_GET["cid"]);
$city = $live['city'];
$body = "{$live['time']}发布<br />\n温度:{$live['temp']}℃<br />\n湿度:{$live['SD']}<br />\n风向:{$live['WD']}<br />\n风力:{$live['WS']}<br />\n";
}
function getlivedata($cid) {
if (!function_exists('curl_init')) {
do {
$data = file_get_contents('http://www.weather.com.cn/data/sk/'.$cid.'.html');
} while ($data == '');
}
else {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.weather.com.cn/data/sk/'.$cid.'.html');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
do {
$data = curl_exec($ch);
} while ($data == '');
curl_close($ch);
}
$data = json_decode($data, TRUE);
return $data['weatherinfo'];
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title><?php echo $city; ?>天气实况</title>
</head>
<body>
<?php echo $body; ?>
</body>
</html>