关于api:基于聚合数据的免费天气预报接口查询PHP版

41次阅读

共计 3137 个字符,预计需要花费 8 分钟才能阅读完成。

1、开明接口

天气预报接口服务应用的聚合数据提供的收费接口,每天能够 100 次收费调用。能够通过 https://www.juhe.cn/docs/api/id/73 注册及开明。

2、通过 PHP 发动城市天气查问

<?php
// 申请的接口 URL
$apiUrl = 'http://apis.juhe.cn/simpleWeather/query';
// 申请参数
$params = [
    'city' => '苏州', // 要查问的城市
    'key' => '您申请的接口调用 Key'
];
$paramsString = http_build_query($params);

// 发动接口网络申请
$response = juheHttpRequest($apiUrl, $paramsString , 1);
$result = json_decode($response, true);
if ($result) {$errorCode = $result['error_code'];
    if ($errorCode == 0) {
        // 获取返回的天气相干信息,具体依据业务理论逻辑调整批改
        $data = $result['result'];

        // 打印以后实况天气信息
        echo "以后城市:{$data["city"]}".PHP_EOL;
        echo "以后温度:{$data["realtime"]["temperature"]}".PHP_EOL;
        echo "以后湿度:{$data["realtime"]["humidity"]}".PHP_EOL;
        echo "以后天气:{$data["realtime"]["info"]}".PHP_EOL;
        echo "以后风向:{$data["realtime"]["direct"]}".PHP_EOL;
        echo "以后风力:{$data["realtime"]["power"]}".PHP_EOL;
        echo "以后空气质量:{$data["realtime"]["aqi"]}".PHP_EOL;

    } else {
        // 申请异样
        echo "申请异样:{$errorCode}_{$result["reason"]}".PHP_EOL;
    }
} else {
    // 可能网络异样等问题,无奈失常取得相应内容,业务逻辑可自行批改
    echo "申请异样".PHP_EOL;
}

/**
 * 发动网络申请函数
 * @param $url 申请的 URL
 * @param bool $params 申请的参数内容
 * @param int $ispost 是否 POST 申请
 * @return bool|string 返回内容
 */
function juheHttpRequest($url, $params = false, $ispost = 0)
{$httpInfo = array();
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_TIMEOUT, 12);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($ispost) {curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {if ($params) {curl_setopt($ch, CURLOPT_URL, $url.'?'.$params);
        } else {curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    $response = curl_exec($ch);
    if ($response === FALSE) {// echo "cURL Error:".curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}

3、通过 PHP 发动生存指数查问

// 申请的接口 URL
$apiUrl = 'http://apis.juhe.cn/simpleWeather/life';
// 申请参数
$params = [
    'city' => '苏州', // 要查问的城市
    'key' => '您申请的接口调用 Key'
];
$paramsString = http_build_query($params);

// 发动接口网络申请
$response = juheHttpRequest($apiUrl, $paramsString , 1);
$result = json_decode($response, true);
if ($result) {$errorCode = $result['error_code'];
    if ($errorCode == 0) {
        // 获取返回的天气相干信息,具体依据业务理论逻辑调整批改
        $data = $result['result']['life'];

        // 打印以后实况天气信息
        echo "空调指数:{$data['kongtiao']['v']},{$data['kongtiao']['des']}".PHP_EOL;
        echo "过敏指数:{$data['guomin']['v']},{$data['guomin']['des']}".PHP_EOL;
        echo "舒适度指数:{$data['shushidu']['v']},{$data['shushidu']['des']}".PHP_EOL;
        echo "穿衣指数:{$data['chuanyi']['v']},{$data['chuanyi']['des']}".PHP_EOL;
        echo "钓鱼指数:{$data['diaoyu']['v']},{$data['diaoyu']['des']}".PHP_EOL;
        echo "感冒指数:{$data['ganmao']['v']},{$data['ganmao']['des']}".PHP_EOL;
        echo "紫外线指数:{$data['ziwaixian']['v']},{$data['ziwaixian']['des']}".PHP_EOL;
        echo "洗车指数:{$data['xiche']['v']},{$data['xiche']['des']}".PHP_EOL;
        echo "静止指数:{$data['yundong']['v']},{$data['yundong']['des']}".PHP_EOL;
        echo "带伞指数:{$data['daisan']['v']},{$data['daisan']['des']}".PHP_EOL;
        // .......

    } else {
        // 申请异样
        echo "申请异样:{$errorCode}_{$result["reason"]}".PHP_EOL;
    }
} else {
    // 可能网络异样等问题,无奈失常取得相应内容,业务逻辑可自行批改
    echo "申请异样".PHP_EOL;
}

正文完
 0