Code description: php-based water quality Query api call code example Associated Data: Water quality Interface address: http://www.juhe.cn/docs/api/id/34
-
- // + ----------------------------------------------------------------------
-
- //----------------------------------
- // Sample code of water quality call-aggregate data
- // Online interface documentation: http://www.juhe.cn/docs/34
- //----------------------------------
-
- Header ('content-type: text/html; charset = utf-8 ');
-
-
- // Configure the appkey you applied
- $ Appkey = "*********************";
-
-
-
-
- // ************ 1. query the water quality of a watershed ************
- $ Url = "http://web.juhe.cn: 8080/environment/water/river ";
- $ Params = array (
- "River" => "", // watershed name. if the query watershed is "Yangtze river Basin", 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. the monitoring site queries the water quality ************
- $ Url = "http://web.juhe.cn: 8080/environment/water/state ";
- $ Params = array (
- "State" => "", // monitoring site name. if the query site is "Yichang Nanjinguan, Hubei province", enter "Yichang Nanjinguan, Hubei province"
- "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, // APPKEY of the application
- );
- $ 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 ";
- }
- //************************************** ************
-
-
-
-
-
- /**
- * Content returned by the request interface
- * @ Param string $ url [requested URL]
- * @ Param string $ params [request parameters]
- * @ Param int $ ipost [whether to use the 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;
- }
|