123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace app\api\controller\screen;
- use app\hander\HelpHander;
- use think\Controller;
- use think\Db;
- use think\Exception;
- /**
- * 天气接口
- */
- class Weather extends Controller
- {
- //根据ip 获取城市
- public function address() {
- $getIp = request()->ip();//获取真实IP
- $url = "http://apis.juhe.cn/ip/ipNewV3";
- $param = [
- "key" => "40d20d107ede1653b5c3260d639362e3",
- "ip" => $getIp
- ];
- $content = $this->juheHttpRequest($url,$param);
- $address = json_decode($content);
- $address = $address->result;
- $return['province'] = $address->Province;//省份
- $return['city'] = $address->City;//城市
- $return['district'] = $address->District;//区
- return $return;
- }
- //根据城市获取当天天气
- public function getTodayWeather(){
- try {
- // 请求的接口URL
- $apiUrl = 'http://apis.juhe.cn/simpleWeather/query';
- // 请求参数
- $addr = $this->address();
- $params = [
- 'city' => $addr['city'], // 要查询的城市
- 'key' => config('app.weather_key')
- ];
- $paramsString = http_build_query($params);
- // 发起接口网络请求
- $response = $this->juheHttpRequest($apiUrl, $paramsString , 1);
- $result = json_decode($response, true);
- $data = [];
- if($result){
- $temperature = '';
- if($result['result']['realtime']['temperature']){
- $temperature =$result['result']['realtime']['temperature'].'℃';
- }
- $wid = $result['result']['realtime']['wid'];
- $data = [
- 'city'=>$addr['city'],
- 'direct'=>$result['result']['realtime']['info'],
- 'temperature'=>$temperature,
- 'img'=>config("app.app_host").'/admin/weather/'.$wid.'.png',
- ];
- }
- HelpHander::success($data);
- }catch (Exception $e){
- HelpHander::success($data = [
- 'city'=>'',
- 'direct'=>'',
- 'temperature'=>'',
- 'img'=>''
- ]);
- }
- }
- /**
- * 发起网络请求函数
- * @param $url 请求的URL
- * @param bool $params 请求的参数内容
- * @param int $ispost 是否POST请求
- * @return bool|string 返回内容
- */
- function juheHttpRequest($url, $params = false, $ispost = 0)
- {
- $httpInfo = array();
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- 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');
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
- curl_setopt($ch, CURLOPT_TIMEOUT, 12);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // curl_setopt($ch, CURLOPT_PROXY, 'http://10.10.134.94:3128');
- if ($ispost) {
- curl_setopt($ch, CURLOPT_POST, true);
- if($params){
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- } else {
- if ($params) {
- curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($params));
- } else {
- curl_setopt($ch, CURLOPT_URL, $url);
- }
- }
- $response = curl_exec($ch);
- if ($response === FALSE) {
- // echo "cURL Error: ".curl_error($ch);
- return false;
- }
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
- curl_close($ch);
- return $response;
- }
- }
|