{ "HeWeather6": [{ "basic": { "cid": "CN101010100", "location": "北京", "parent_city": "北京", "admin_area": "北京", "cnty": "中国", "lat": "39.90498734", "lon": "116.4052887", "tz": "+8.00" }, "update": { "loc": "2020-01-08 17:15", "utc": "2020-01-08 09:15" }, "status": "ok", "air_now_city": { "aqi": "21", "qlty": "优", "main": "-", "pm25": "13", "pm10": "17", "no2": "18", "so2": "3", "co": "0.4", "o3": "65", "pub_time": "2020-01-08 16:00" }, "air_now_station": [{ "air_sta": "万寿西宫", "aqi": "30", "asid": "CNA1001", "co": "0.4", "lat": "39.8673", "lon": "116.366", "main": "-", "no2": "32", "o3": "48", "pm10": "30", "pm25": "18", "pub_time": "2020-01-08 16:00", "qlty": "优", "so2": "1" }] }] }
<?php /** * Created by PhpStorm. * User: FZS * Time: 2020/01/06 22:16 */ //---------------------------------- // 空气质量 调用类 //---------------------------------- class freeApi{ private $apiUrl; private $appKey; public function __construct($appKey){ $this->appKey = $appKey; $this->apiUrl = 'https://free-api.heweather.net/s6/air/now'; } /** * 获取结果 * @return array */ public function getResult(){ $paras = [ 'key' => $this->appKey, 'location' => 'beijing', ]; return $this->freeApiCurl($this->apiUrl,$paras); } /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ public function freeApiCurl($url,$params=false){ $headers = array('Content-Type: application/x-www-form-urlencoded'); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params)); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno'.curl_error($curl);//捕抓异常 } curl_close($curl); // 关闭CURL会话 return $result; } }
package main import ( "fmt" "io/ioutil" "log" "net/http" ) const ( APIURL = "https://free-api.heweather.net/s6/air/now" APIKEY = "your key" ) func main() { queryUrl := fmt.Sprintf("%s?key=%s&location=beijing",APIURL,APIKEY) resp, err := http.Get(queryUrl) if err != nil { log.Println(err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return } fmt.Println(string(body)) }