heweatherAdapter.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Heanup\Library\WeatherAdapt;
  3. use Heanup\App\Api\Config\GlobalConfig;
  4. use Heanup\Library\Curl;
  5. use Heanup\Library\Redis;
  6. use Heanup\Library\Warming;
  7. class heweatherAdapter implements Weather
  8. {
  9. private $city_id;
  10. /**
  11. * @param $city_id
  12. * @return mixed
  13. */
  14. public function getWeatherData($city_id)
  15. {
  16. $this->city_id = $city_id;
  17. $city_id = is_numeric($city_id) ? ("CN" . $city_id) : $city_id;
  18. $key = $this->getKey();
  19. if ($key) {
  20. $url = sprintf("https://api.heweather.net/s6/weather?location=%s&key=%s", $city_id, $key);
  21. }
  22. $data = Curl::get($url);
  23. $origin = $data = json_decode($data, true);
  24. $data = $this->parseData($data);
  25. $i = 0;
  26. while ($data['Weather']['now']['cond_txt'] == "" && $i < 2) {//数据有误,重试3次
  27. $log_file=APP_DIR . "/logs/" . date("Ymd") . "_null.log";
  28. 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);
  29. Warming::semdMail("no_data", "api.heweather.net", ALERT_TOTAL,$log_file);
  30. $data = Curl::get($url);
  31. $origin = $data = json_decode($data, true);
  32. $data = $this->parseData($data);
  33. $i++;
  34. }
  35. return $data;
  36. }
  37. private function getKey()
  38. {
  39. //2019/07/11 接口切换为收费接口,不再限制使用次数
  40. // $day = "keys_use_" . date("Ymd");
  41. // $count = Redis::instance()->incr($day);
  42. $keys = GlobalConfig::getData("keys");
  43. // if ($count / 16000 > count($keys)) {
  44. // //报警
  45. // Warming::semdMail("key_usage", "free-api.heweather.net", 1);
  46. // }
  47. // $key_count = intval($count / 16000);
  48. $key = date("Ymd_") . $keys[0];
  49. Redis::instance()->incr($key);
  50. // $keys = [
  51. // "_1" => '2b33a509d3134c2a8844e3ce1472d3bd',
  52. // "_2" => 'f576a47c99aa4746b7145c4bc3138316',
  53. // "_3" => '420475fd0e77489c843ef389b733bb48',
  54. // "_4" => 'ce36b0498e444bc98c5bed07180828b3',
  55. // ];
  56. return $keys[0];
  57. }
  58. /**
  59. *
  60. * @param $data
  61. * @param null $data2
  62. * @return array
  63. */
  64. public function parseData($data, $data2 = null)
  65. {
  66. $originData = $data['HeWeather6'][0];
  67. $return = [];
  68. $weather = [];
  69. $weather['basic'] = [
  70. 'location' => $originData['basic']['location'] ?: "",
  71. 'city_code' => $this->city_id,
  72. 'update' => $originData['update']['loc'] ?: "",
  73. ];
  74. $weather['status'] = $originData['status'] ?: "ok";
  75. // $weather['key'] = $k;
  76. $weather['air_now_city'] = [
  77. "aqi" => "", // ->a3
  78. "qlty" => "", // ->a4
  79. "pm25" => "", // ->a5
  80. "pm10" => "", // ->a6
  81. "no2" => "", // ->a7
  82. "so2" => ""
  83. ];
  84. $weather['alarm'] = [
  85. "level" => "", // ->a9
  86. "link" => "", // ->b1
  87. "type" => "" // ->b2
  88. ];
  89. $weather['now'] = [
  90. "cond_txt" => $originData['now']['cond_txt'] ?: "", // ->b3
  91. "fl" => $originData['now']['fl'] ?: "",// ->b4
  92. "hum" => $originData['now']['hum'] ?: "", // ->b5
  93. "pres" => $originData['now']['pres'] ?: "", // ->b6
  94. "tmp" => $originData['now']['tmp'] ?: "", // ->b7
  95. "vis" => $originData['now']['vis'] ?: "", // ->b8
  96. "wind_dir" => $originData['now']['wind_dir'] ?: "", // ->b9
  97. "wind_sc" => $originData['now']['wind_sc'] ?: "", // ->c1
  98. "sr" => $originData['daily_forecast'][0]['sr'] ?: "", // ->c2
  99. "ss" => $originData['daily_forecast'][0]['ss'] ?: "", // ->c3
  100. ];
  101. if (!empty($originData['daily_forecast'])) {
  102. foreach ($originData['daily_forecast'] as $key => $value) {
  103. $temp = [
  104. "cond_txt_d" => $value['cond_txt_d'] ?: "",
  105. "cond_txt_n" => $value['cond_txt_n'] ?: "",
  106. "date" => $value['date'] ?: "",
  107. "tmp_max" => $value['tmp_max'] ?: "",
  108. "tmp_min" => $value['tmp_min'] ?: "",
  109. "wind_dir" => $value['wind_dir'] ?: "",
  110. "wind_sc" => $value['wind_sc'] ?: "",
  111. ];
  112. $weather['daily_forecast'][] = $temp;
  113. }
  114. }
  115. if (!empty($originData['hourly'])) {
  116. foreach ($originData['hourly'] as $key => $value) {
  117. $temp = [
  118. 'cond_txt' => $value['cond_txt'] ?: "",
  119. 'time' => $value['time'] ?: "",
  120. 'tmp' => $value['tmp'] ?: "",
  121. ];
  122. $weather['hourly'][] = $temp;
  123. }
  124. }
  125. if (!empty($originData['lifestyle'])) {
  126. foreach ($originData['lifestyle'] as $key => $value) {
  127. $temp = [
  128. 'type' => $value['type'] ?: "",
  129. 'brf' => $value['brf'] ?: "",
  130. 'txt' => $value['txt'] ?: "",
  131. ];
  132. $weather['lifestyle'][] = $temp;
  133. }
  134. }
  135. $return['Weather'] = $weather;
  136. $return['source'] = "heweather.net";
  137. return $return;
  138. }
  139. }