人人商城

weather.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. $message = $this->message;
  3. $ret = preg_match('/(.+)天气/i', $this->message['content'], $matchs);
  4. if(!$ret) {
  5. return $this->respText('请输入合适的格式, 城市+天气, 例如: 北京天气');
  6. }
  7. $city = $matchs[1];
  8. $response = array();
  9. $url = 'https://way.jd.com/he/freeweather?city=%s&appkey=f8a020a5d840f7a24997c9561b7d3da3';
  10. $obj = weather_http_request($url, $city);
  11. $data = $city . '今日天气:' . $obj['cond']['txt'] . PHP_EOL .
  12. '空气质量:' . $obj['aqi']['city']['qlty'] . PHP_EOL .
  13. 'pm25:' . $obj['aqi']['city']['pm25'] . PHP_EOL .
  14. '温度:' . $obj['now']['tmp'] . '摄氏度。' . PHP_EOL .
  15. '湿度:' . $obj['now']['hum'] . PHP_EOL .
  16. '能见度:' . $obj['now']['vis'] . PHP_EOL .
  17. '降水量:' . $obj['now']['pcpn'] . PHP_EOL .
  18. '感冒指数:' . $obj['now']['fl'] . PHP_EOL .
  19. '风级:' . $obj['now']['wind']['sc'] . PHP_EOL .
  20. '风向:' . $obj['now']['wind']['dir'] . PHP_EOL;
  21. $response = $this->respText($data);
  22. return $response;
  23. function weather_http_request($url, $city) {
  24. $url = sprintf($url, $city);
  25. $resp = ihttp_get($url);
  26. if ($resp['code'] == 200 && $resp['content']) {
  27. $obj = json_decode($resp['content'], true);
  28. return $obj['result']['HeWeather5'][0];
  29. }
  30. return '';
  31. }