Weather.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\api\controller\screen;
  3. use app\hander\HelpHander;
  4. use think\Controller;
  5. use think\Db;
  6. /**
  7. * 天气接口
  8. */
  9. class Weather extends Controller
  10. {
  11. //根据ip 获取城市
  12. public function address() {
  13. $getIp = request()->ip();//获取真实IP
  14. // $getIp = '120.46.129.222';//获取真实IP
  15. $content = file_get_contents("http://apis.juhe.cn/ip/ipNewV3?key=dd24ed2a3ba02ca20d6eec1d6175cb7c&ip={$getIp}");
  16. $address = json_decode($content);
  17. $address = $address->result;
  18. $return['province'] = $address->Province;//省份
  19. $return['city'] = $address->City;//城市
  20. $return['district'] = $address->District;//城市
  21. return $return;
  22. }
  23. //根据城市获取当天天气
  24. public function getTodayWeather(){
  25. // 请求的接口URL
  26. $apiUrl = 'http://apis.juhe.cn/simpleWeather/query';
  27. // 请求参数
  28. $addr = $this->address();
  29. $params = [
  30. 'city' => $addr['city'], // 要查询的城市
  31. 'key' => config('app.weather_key')
  32. ];
  33. $paramsString = http_build_query($params);
  34. // 发起接口网络请求
  35. $response = $this->juheHttpRequest($apiUrl, $paramsString , 1);
  36. $result = json_decode($response, true);
  37. $data = [];
  38. if($result){
  39. $temperature = '';
  40. if($result['result']['realtime']['temperature']){
  41. $temperature =$result['result']['realtime']['temperature'].'℃';
  42. }
  43. $wid = $result['result']['realtime']['wid'];
  44. $data = [
  45. 'city'=>$addr['city'],
  46. 'direct'=>$result['result']['realtime']['info'],
  47. 'temperature'=>$temperature,
  48. 'img'=>getSite().'/admin/weather/'.$wid.'.png',
  49. ];
  50. }
  51. HelpHander::success($data);
  52. }
  53. /**
  54. * 发起网络请求函数
  55. * @param $url 请求的URL
  56. * @param bool $params 请求的参数内容
  57. * @param int $ispost 是否POST请求
  58. * @return bool|string 返回内容
  59. */
  60. function juheHttpRequest($url, $params = false, $ispost = 0)
  61. {
  62. $httpInfo = array();
  63. $ch = curl_init();
  64. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  65. 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');
  66. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  67. curl_setopt($ch, CURLOPT_TIMEOUT, 12);
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  69. // curl_setopt($ch, CURLOPT_PROXY, 'http://10.10.134.94:3128');
  70. if ($ispost) {
  71. curl_setopt($ch, CURLOPT_POST, true);
  72. if($params){
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  74. }
  75. curl_setopt($ch, CURLOPT_URL, $url);
  76. } else {
  77. if ($params) {
  78. curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($params));
  79. } else {
  80. curl_setopt($ch, CURLOPT_URL, $url);
  81. }
  82. }
  83. $response = curl_exec($ch);
  84. if ($response === FALSE) {
  85. // echo "cURL Error: ".curl_error($ch);
  86. return false;
  87. }
  88. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  89. $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
  90. curl_close($ch);
  91. return $response;
  92. }
  93. }