Code description: PHP-based water quality query api calling code example Associated data: water quality Interface address: http://www.juhe.cn/docs/api/id/34
- // +------------- -------------------------------------------------- --------
-
- //----------------------------------
- // Water Quality call sample code - aggregated data
- // Online interface documentation: http://www.juhe.cn/docs/34
- //-------------------------- ---------------
-
- header('Content-type:text/html;charset=utf-8');
-
-
- //Configure the appkey you applied for
- $appkey = " *********************";
-
-
-
-
- //************1. Query water quality in river basins************
- $url = "http://web.juhe.cn:8080/environment/water/river ";
- $params = array(
- "river" => "",//Basin name, if the query basin is "Yangtze River Basin", then enter "Yangtze River Basin"
- "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 "Request failed";
- }
- //************************************ ******************
-
-
-
-
- //************2. Monitoring site to check water quality********** **
- $url = "http://web.juhe.cn:8080/environment/water/state";
- $params = array(
- "state" => "",//Monitoring site name, query site If it is "Nanjinguan, Yichang, Hubei", then enter "Nanjinguan, Yichang, Hubei"
- "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 "Request failed";
- }
- //******* *******************************************
-
-
-
-
- // ************3. Monitoring site list************
- $url = "http://web.juhe.cn:8080/environment/water/ stateList";
- $params = array(
- "key" => $appkey,//Application APPKEY
- );
- $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 "Request failed";
- }
- //************** ************************************
-
-
-
-
-
- /**
- * Request interface return Content
- * @param string $url [requested URL address]
- * @param string $params [requested parameters]
- * @param int $ipost [whether to use POST form]
- * @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;
- }
Copy code
|