Php-based water quality Query api call code example

Source: Internet
Author: User
Php-based water quality Query api call code example
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

  1. // + ----------------------------------------------------------------------
  2. //----------------------------------
  3. // Sample code of water quality call-aggregate data
  4. // Online interface documentation: http://www.juhe.cn/docs/34
  5. //----------------------------------
  6. Header ('content-type: text/html; charset = utf-8 ');
  7. // Configure the appkey you applied
  8. $ Appkey = "*********************";
  9. // ************ 1. query the water quality of a watershed ************
  10. $ Url = "http://web.juhe.cn: 8080/environment/water/river ";
  11. $ Params = array (
  12. "River" => "", // watershed name. if the query watershed is "Yangtze river Basin", enter "Yangtze river Basin"
  13. "Key" => $ appkey, // APP Key
  14. );
  15. $ Paramstring = http_build_query ($ params );
  16. $ Content = juhecurl ($ url, $ paramstring );
  17. $ Result = json_decode ($ content, true );
  18. If ($ result ){
  19. If ($ result ['error _ Code'] = '0 '){
  20. Print_r ($ result );
  21. } Else {
  22. Echo $ result ['error _ Code']. ":". $ result ['reason '];
  23. }
  24. } Else {
  25. Echo "request failed ";
  26. }
  27. //************************************** ************
  28. // ************ 2. the monitoring site queries the water quality ************
  29. $ Url = "http://web.juhe.cn: 8080/environment/water/state ";
  30. $ Params = array (
  31. "State" => "", // monitoring site name. if the query site is "Yichang Nanjinguan, Hubei province", enter "Yichang Nanjinguan, Hubei province"
  32. "Key" => $ appkey, // APP Key
  33. );
  34. $ Paramstring = http_build_query ($ params );
  35. $ Content = juhecurl ($ url, $ paramstring );
  36. $ Result = json_decode ($ content, true );
  37. If ($ result ){
  38. If ($ result ['error _ Code'] = '0 '){
  39. Print_r ($ result );
  40. } Else {
  41. Echo $ result ['error _ Code']. ":". $ result ['reason '];
  42. }
  43. } Else {
  44. Echo "request failed ";
  45. }
  46. //************************************** ************
  47. // ************ 3. monitoring site list ************
  48. $ Url = "http://web.juhe.cn: 8080/environment/water/stateList ";
  49. $ Params = array (
  50. "Key" => $ appkey, // APPKEY of the application
  51. );
  52. $ Paramstring = http_build_query ($ params );
  53. $ Content = juhecurl ($ url, $ paramstring );
  54. $ Result = json_decode ($ content, true );
  55. If ($ result ){
  56. If ($ result ['error _ Code'] = '0 '){
  57. Print_r ($ result );
  58. } Else {
  59. Echo $ result ['error _ Code']. ":". $ result ['reason '];
  60. }
  61. } Else {
  62. Echo "request failed ";
  63. }
  64. //************************************** ************
  65. /**
  66. * Content returned by the request interface
  67. * @ Param string $ url [requested URL]
  68. * @ Param string $ params [request parameters]
  69. * @ Param int $ ipost [whether to use the POST form]
  70. * @ Return string
  71. */
  72. Function juhecurl ($ url, $ params = false, $ ispost = 0 ){
  73. $ HttpInfo = array ();
  74. $ Ch = curl_init ();
  75. Curl_setopt ($ ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
  76. Curl_setopt ($ ch, CURLOPT_USERAGENT, 'juhedata ');
  77. Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 60 );
  78. Curl_setopt ($ ch, CURLOPT_TIMEOUT, 60 );
  79. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
  80. Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true );
  81. If ($ ispost)
  82. {
  83. Curl_setopt ($ ch, CURLOPT_POST, true );
  84. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ params );
  85. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  86. }
  87. Else
  88. {
  89. If ($ params ){
  90. Curl_setopt ($ ch, CURLOPT_URL, $ url .'? '. $ Params );
  91. } Else {
  92. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  93. }
  94. }
  95. $ Response = curl_exec ($ ch );
  96. If ($ response = FALSE ){
  97. // Echo "cURL Error:". curl_error ($ ch );
  98. Return false;
  99. }
  100. $ HttpCode = curl_getinfo ($ ch, CURLINFO_HTTP_CODE );
  101. $ HttpInfo = array_merge ($ httpInfo, curl_getinfo ($ ch ));
  102. Curl_close ($ ch );
  103. Return $ response;
  104. }

Php, api

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.