php查詢城市空氣質(zhì)量的方法:1、開(kāi)通空氣質(zhì)量API接口;2、創(chuàng)建php示例文件;3、設(shè)置文件編碼為“utf-8”;4、配置申請(qǐng)的appkey;5、請(qǐng)求接口URL;6、通過(guò)“$params = ['city' => '…', 'key' => '…'];”方式請(qǐng)求城市空氣質(zhì)量相關(guān)參數(shù)信息即可。
php入門(mén)到就業(yè)線上直播課:進(jìn)入學(xué)習(xí)
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點(diǎn)擊使用
本教程操作環(huán)境:windows7系統(tǒng)、PHP8.1版、Dell G3電腦。
php如何查詢城市空氣質(zhì)量?
1、開(kāi)通空氣質(zhì)量API接口:
-
通過(guò)
https://www.juhe.cn/docs/api/id/33?s=cpphpcn
注冊(cè)及開(kāi)通
接口說(shuō)明:
-
支持全國(guó)大部分城市空氣質(zhì)量查詢,可實(shí)時(shí)查詢空氣質(zhì)量,小時(shí)粒度,實(shí)時(shí)給出空氣質(zhì)量AQI指數(shù),并給出空氣質(zhì)量級(jí)別和首要污染物。
-
有的城市可能沒(méi)有監(jiān)測(cè)點(diǎn)實(shí)時(shí)監(jiān)測(cè)的數(shù)據(jù),每1小時(shí)更新一次。
2、基于php的空氣質(zhì)量接口調(diào)用示例
php代碼如下:
// 空氣質(zhì)量調(diào)用示例代碼 header('Content-type:text/html;charset=utf-8'); //配置您申請(qǐng)的appkey $appkey = "*********************"; //************1.城市空氣質(zhì)量************ $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 "請(qǐng)求失敗"; } //************************************************** //************2.城市空氣PM2.5指數(shù)************ $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 "請(qǐng)求失敗"; } //************************************************** //************3.城市空氣質(zhì)量-城市列表************ $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 "請(qǐng)求失敗"; } //************************************************** //************4.城市空氣PM2.5指數(shù)-城市列表************ $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 "請(qǐng)求失敗"; } //************************************************** //************5.城市輻射指數(shù)************ $url = "http://web.juhe.cn:8080/environment/air/radia"; $params = array( "city" => "",//城市名稱的中文拼音,查詢城市為“上?!保瑒t輸入:上海 "num" => "",//查詢頁(yè)碼數(shù),不寫(xiě)默認(rèn)為第一頁(yè)。 "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 "請(qǐng)求失敗"; } //************************************************** /** * 請(qǐng)求接口返回內(nèi)容 * @param string $url [請(qǐng)求的URL地址] * @param string $params [請(qǐng)求的參數(shù)] * @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; }
推薦學(xué)習(xí):《PHP視頻教程》