php查詢城市空氣質量的方法:1、開通空氣質量API接口;2、創建php示例文件;3、設置文件編碼為“utf-8”;4、配置申請的appkey;5、請求接口URL;6、通過“$params = ['city' => '…', 'key' => '…'];”方式請求城市空氣質量相關參數信息即可。
php入門到就業線上直播課:進入學習
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調試工具:點擊使用
本教程操作環境:windows7系統、PHP8.1版、Dell G3電腦。
php如何查詢城市空氣質量?
1、開通空氣質量API接口:
-
通過
https://www.juhe.cn/docs/api/id/33?s=cpphpcn
注冊及開通
接口說明:
-
支持全國大部分城市空氣質量查詢,可實時查詢空氣質量,小時粒度,實時給出空氣質量AQI指數,并給出空氣質量級別和首要污染物。
-
有的城市可能沒有監測點實時監測的數據,每1小時更新一次。
2、基于php的空氣質量接口調用示例
php代碼如下:
// 空氣質量調用示例代碼 header('Content-type:text/html;charset=utf-8'); //配置您申請的appkey $appkey = "*********************"; //************1.城市空氣質量************ $url = "http://web.juhe.cn:8080/environment/air/cityair"; $params = array( "city" => "",//城市名稱的中文名稱或拼音,如:上海 或 shanghai "key" => $appkey,//APP Key ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** //************2.城市空氣PM2.5指數************ $url = "http://web.juhe.cn:8080/environment/air/pm"; $params = array( "city" => "",//城市名稱的中文名稱或拼音,如:上海 或 shanghai "key" => $appkey,//APP Key ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** //************3.城市空氣質量-城市列表************ $url = "http://web.juhe.cn:8080/environment/air/airCities"; $params = array( "key" => $appkey,//APP Key ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** //************4.城市空氣PM2.5指數-城市列表************ $url = "http://web.juhe.cn:8080/environment/air/pmCities"; $params = array( "key" => $appkey,//APP Key ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** //************5.城市輻射指數************ $url = "http://web.juhe.cn:8080/environment/air/radia"; $params = array( "city" => "",//城市名稱的中文拼音,查詢城市為“上?!?,則輸入:上海 "num" => "",//查詢頁碼數,不寫默認為第一頁。 "key" => $appkey,//APP Key ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "請求失敗"; } //************************************************** /** * 請求接口返回內容 * @param string $url [請求的URL地址] * @param string $params [請求的參數] * @param int $ipost [是否采用POST形式] * @return string */ function juhecurl($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 , 'JuheData' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); if( $ispost ) { curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); } else { if($params){ curl_setopt( $ch , CURLOPT_URL , $url.'?'.$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; }
登錄后復制
推薦學習:《PHP視頻教程》