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

        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號
        主站蜘蛛池模板: 日本人精品video黑人| 国内精品久久久久久久coent| 亚洲国产精品国自产拍电影| 亚洲午夜成人精品电影在线观看 | 日韩经典精品无码一区| 久久成人国产精品二三区| 久久久一本精品99久久精品88| 久久精品国产精品亚洲艾草网美妙| 中文字幕精品一区二区日本| 国产精品v片在线观看不卡| 午夜精品久久久久久99热| 亚洲精品456播放| 久久精品国产精品亚洲| 国产精品日韩深夜福利久久| 久久青草国产精品一区| 国产成人精品日本亚洲网址| 国产欧美日韩精品a在线观看| 伊人 久久 精品| 人妻无码久久精品| 久久久久九国产精品| 精品国产一区二区三区AV性色| 国产精品推荐天天看天天爽| 99久久精品免费国产大片| 青青草国产精品久久| 欧美亚洲精品在线| 真实国产乱子伦精品免费| 亚洲综合精品香蕉久久网97| 9999国产精品欧美久久久久久| 丁香色婷婷国产精品视频| 国产999精品久久久久久| 久久综合九色综合精品| 丝袜美腿国产精品视频一区| 性色精品视频网站在线观看| 五月花精品视频在线观看| 在线观看91精品国产入口| 国产精品成人啪精品视频免费| 国产成人精品久久| 国产精品最新国产精品第十页| 国产精品原创巨作?v网站| 精品无码人妻久久久久久| 日韩精品一区二区三区在线观看|