| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace Heanup\Library\WeatherAdapt;
- use Heanup\App\Api\Config\GlobalConfig;
- use Heanup\Library\Curl;
- use Heanup\Library\Redis;
- use Heanup\Library\Warming;
- class heweatherAdapter implements Weather
- {
- private $city_id;
- /**
- * @param $city_id
- * @return mixed
- */
- public function getWeatherData($city_id)
- {
- $this->city_id = $city_id;
- $city_id = is_numeric($city_id) ? ("CN" . $city_id) : $city_id;
- $key = $this->getKey();
- if ($key) {
- $url = sprintf("https://api.heweather.net/s6/weather?location=%s&key=%s", $city_id, $key);
- }
- $data = Curl::get($url);
- $origin = $data = json_decode($data, true);
- $data = $this->parseData($data);
- $i = 0;
- while ($data['Weather']['now']['cond_txt'] == "" && $i < 2) {//数据有误,重试3次
- $log_file=APP_DIR . "/logs/" . date("Ymd") . "_null.log";
- file_put_contents($log_file, sprintf("%s\t\tcity_id:%s", date("Y-m-d H:i:s"), $city_id) . "\t\tdata:" . json_encode($origin) . "\r\n", FILE_APPEND);
- Warming::semdMail("no_data", "api.heweather.net", ALERT_TOTAL,$log_file);
- $data = Curl::get($url);
- $origin = $data = json_decode($data, true);
- $data = $this->parseData($data);
- $i++;
- }
- return $data;
- }
- private function getKey()
- {
- //2019/07/11 接口切换为收费接口,不再限制使用次数
- // $day = "keys_use_" . date("Ymd");
- // $count = Redis::instance()->incr($day);
- $keys = GlobalConfig::getData("keys");
- // if ($count / 16000 > count($keys)) {
- // //报警
- // Warming::semdMail("key_usage", "free-api.heweather.net", 1);
- // }
- // $key_count = intval($count / 16000);
- $key = date("Ymd_") . $keys[0];
- Redis::instance()->incr($key);
- // $keys = [
- // "_1" => '2b33a509d3134c2a8844e3ce1472d3bd',
- // "_2" => 'f576a47c99aa4746b7145c4bc3138316',
- // "_3" => '420475fd0e77489c843ef389b733bb48',
- // "_4" => 'ce36b0498e444bc98c5bed07180828b3',
- // ];
- return $keys[0];
- }
- /**
- *
- * @param $data
- * @param null $data2
- * @return array
- */
- public function parseData($data, $data2 = null)
- {
- $originData = $data['HeWeather6'][0];
- $return = [];
- $weather = [];
- $weather['basic'] = [
- 'location' => $originData['basic']['location'] ?: "",
- 'city_code' => $this->city_id,
- 'update' => $originData['update']['loc'] ?: "",
- ];
- $weather['status'] = $originData['status'] ?: "ok";
- // $weather['key'] = $k;
- $weather['air_now_city'] = [
- "aqi" => "", // ->a3
- "qlty" => "", // ->a4
- "pm25" => "", // ->a5
- "pm10" => "", // ->a6
- "no2" => "", // ->a7
- "so2" => ""
- ];
- $weather['alarm'] = [
- "level" => "", // ->a9
- "link" => "", // ->b1
- "type" => "" // ->b2
- ];
- $weather['now'] = [
- "cond_txt" => $originData['now']['cond_txt'] ?: "", // ->b3
- "fl" => $originData['now']['fl'] ?: "",// ->b4
- "hum" => $originData['now']['hum'] ?: "", // ->b5
- "pres" => $originData['now']['pres'] ?: "", // ->b6
- "tmp" => $originData['now']['tmp'] ?: "", // ->b7
- "vis" => $originData['now']['vis'] ?: "", // ->b8
- "wind_dir" => $originData['now']['wind_dir'] ?: "", // ->b9
- "wind_sc" => $originData['now']['wind_sc'] ?: "", // ->c1
- "sr" => $originData['daily_forecast'][0]['sr'] ?: "", // ->c2
- "ss" => $originData['daily_forecast'][0]['ss'] ?: "", // ->c3
- ];
- if (!empty($originData['daily_forecast'])) {
- foreach ($originData['daily_forecast'] as $key => $value) {
- $temp = [
- "cond_txt_d" => $value['cond_txt_d'] ?: "",
- "cond_txt_n" => $value['cond_txt_n'] ?: "",
- "date" => $value['date'] ?: "",
- "tmp_max" => $value['tmp_max'] ?: "",
- "tmp_min" => $value['tmp_min'] ?: "",
- "wind_dir" => $value['wind_dir'] ?: "",
- "wind_sc" => $value['wind_sc'] ?: "",
- ];
- $weather['daily_forecast'][] = $temp;
- }
- }
- if (!empty($originData['hourly'])) {
- foreach ($originData['hourly'] as $key => $value) {
- $temp = [
- 'cond_txt' => $value['cond_txt'] ?: "",
- 'time' => $value['time'] ?: "",
- 'tmp' => $value['tmp'] ?: "",
- ];
- $weather['hourly'][] = $temp;
- }
- }
- if (!empty($originData['lifestyle'])) {
- foreach ($originData['lifestyle'] as $key => $value) {
- $temp = [
- 'type' => $value['type'] ?: "",
- 'brf' => $value['brf'] ?: "",
- 'txt' => $value['txt'] ?: "",
- ];
- $weather['lifestyle'][] = $temp;
- }
- }
- $return['Weather'] = $weather;
- $return['source'] = "heweather.net";
- return $return;
- }
- }
|