站長資訊網
最全最豐富的資訊網站

php如何查詢城市空氣質量

php查詢城市空氣質量的方法:1、開通空氣質量API接口;2、創建php示例文件;3、設置文件編碼為“utf-8”;4、配置申請的appkey;5、請求接口URL;6、通過“$params = ['city' => '…', 'key' => '…'];”方式請求城市空氣質量相關參數信息即可。

php如何查詢城市空氣質量

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視頻教程》

贊(0)
分享到: 更多 (0)
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
国产亚洲精品美女久久久久久下载 | 日韩免费观看的一级毛片| 四虎必出精品亚洲高清| 精品无码人妻一区二区三区品 | 亚洲AV永久纯肉无码精品动漫| 狠狠色伊人久久精品综合网| 青青草97国产精品免费观看| www.日韩av.com| 女同久久精品国产99国产精品 | 日韩精品无码区免费专区| 国产精品久久国产精品99| 在线视频精品一区| 久久精品熟女亚洲av麻豆| 亚洲国产精品无码久久98| 97久久久精品综合88久久| 四虎国产精品永久在线播放| 91精品国产手机| 91精品国产免费久久国语麻豆| 人人妻人人澡人人爽精品日本| 国产精品丝袜一区二区三区| 中国精品18videosex性中国| 538prom精品视频我们不只是| 91久久精品91久久性色| 国产亚洲精品a在线观看 | 十八禁无遮挡99精品国产| 中文字幕一区日韩精品| 91情侣在线精品国产免费| 亚洲AV无码成人精品区狼人影院| 国内精品自线在拍2020不卡| 国产69精品久久久久观看软件| 2021久久精品免费观看| 国产手机精品一区二区| 国产99久久久国产精品小说 | 久久se精品一区二区影院 | 国产产无码乱码精品久久鸭| 免费精品99久久国产综合精品| 久久精品岛国av一区二区无码| 久久精品隔壁老王影院| 人妻少妇偷人精品无码| 3d动漫精品啪啪一区二区免费| 91久久精品国产免费直播|