Weather.php 3.5 KB

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