Weather.php 3.8 KB

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