Exemplo n.º 1
0
<?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>
Exemplo n.º 2
0
<?php 
//cid=101010100&callback=cb
$callback = isset($_GET['callback']) ? $_GET['callback'] : '';
if (!isset($_GET['cid']) || strlen($_GET['cid']) != 9 || !ctype_digit($_GET['cid'])) {
    $body = '<h3>城市代码不合法!</h3>';
} else {
    $body = getlivedata($callback, $_GET["cid"]);
}
echo $body;
function getlivedata($callback, $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);
    $data = json_encode($data['weatherinfo']);
    if (!empty($callback)) {
        $jsonp = $callback . '(' . $data . ')';
        return $jsonp;
    }