浏览代码

修复:天气返回最高温度不带℃

刘忠健 9 月之前
父节点
当前提交
6a53ddd4e0
共有 1 个文件被更改,包括 33 次插入26 次删除
  1. 33 26
      api/app/api/controller/Weather.php

+ 33 - 26
api/app/api/controller/Weather.php

@@ -14,36 +14,36 @@ class Weather extends Base
         //仁化 101280206
         //清远 101281301
         //北京 101010100
-        $cityNo=input("city","101280206");
-        $url = "http://www.weather.com.cn/weather/".$cityNo.".shtml";
+        $cityNo = input("city", "101280206");
+        $url = "http://www.weather.com.cn/weather/" . $cityNo . ".shtml";
 //        $ql=QueryList::getInstance()->get($url);
-        $options=[
+        $options = [
             'headers' => [
                 // 携带cookie
-                "Accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
-                "Accept-Encoding"=>"gzip, deflate",
-                "Accept-Language"=>"zh-CN,zh;q=0.9,en;q=0.8",
-                "Cache-Control"=>"no-cache",
-                "Cookie"=>"userNewsPort0=1; defaultCty=101280201; f_city=%E9%9F%B6%E5%85%B3%7C101280201%7C; defaultCtyName=%u97F6%u5173",
-                "DNT"=>"1",
-                "Host"=>"www.weather.com.cn",
-                "Pragma"=>"no-cache",
-                "Proxy-Connection"=>"keep-alive",
-                "Referer"=>"http://www.weather.com.cn/weather1d/101280201.shtml",
-                "Upgrade-Insecure-Requests"=>"1",
-                "User-Agent"=>"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
+                "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
+                "Accept-Encoding" => "gzip, deflate",
+                "Accept-Language" => "zh-CN,zh;q=0.9,en;q=0.8",
+                "Cache-Control" => "no-cache",
+                "Cookie" => "userNewsPort0=1; defaultCty=101280201; f_city=%E9%9F%B6%E5%85%B3%7C101280201%7C; defaultCtyName=%u97F6%u5173",
+                "DNT" => "1",
+                "Host" => "www.weather.com.cn",
+                "Pragma" => "no-cache",
+                "Proxy-Connection" => "keep-alive",
+                "Referer" => "http://www.weather.com.cn/weather1d/101280201.shtml",
+                "Upgrade-Insecure-Requests" => "1",
+                "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
             ]
         ];
-        $html = QueryList::getInstance()->get($url,[],$options)->getHtml();
+        $html = QueryList::getInstance()->get($url, [], $options)->getHtml();
 
 
 //        exit;
         $ql = QueryList::getInstance()->html($html);
-        $city=$ql->find(".crumbs")->text();
-        $city=str_replace(" ","",$city);
-        $city=str_replace("\r","",$city);
-        $city=str_replace("\t","",$city);
-        $city=str_replace("\n","",$city);
+        $city = $ql->find(".crumbs")->text();
+        $city = str_replace(" ", "", $city);
+        $city = str_replace("\r", "", $city);
+        $city = str_replace("\t", "", $city);
+        $city = str_replace("\n", "", $city);
 //        $html7d = $ql->find("#7d>.t")->html();
 //        var_dump($html7d);
         //$ql = QueryList::getInstance()->html($html7d);
@@ -56,14 +56,21 @@ class Weather extends Base
         $rules = [
             "title" => ["h1", "text"],
             "weather" => [".wea", "text"],
-            "max"=>[".tem>span","text"],
-            "min"=>[".tem>i","text"],
+            "max" => [".tem>span", "text"],
+            "min" => [".tem>i", "text"],
         ];
         $range = '#7d>ul>li';
         $weather = $ql->rules($rules)->range($range)->query()->getData()->toArray();
-        $res=[
-            "city"=>$city,
-            "weather"=>$weather
+        //处理最高温度
+        foreach ($weather as $key => $value) {
+            //如果最高温度没包含℃,就加上
+            if (!strpos($value["max"], "℃")) {
+                $weather[$key]["max"] = $weather[$key]["max"] . "℃";
+            }
+        }
+        $res = [
+            "city" => $city,
+            "weather" => $weather
         ];
         $this->success($res);
 //        print_r($res);