Home > Backend Development > PHP Tutorial > PHP-based water quality query api calling code example

PHP-based water quality query api calling code example

WBOY
Release: 2016-07-25 08:43:59
Original
1436 people have browsed it
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
  1. // +------------- -------------------------------------------------- --------
  2. //----------------------------------
  3. // Water Quality call sample code - aggregated 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 for
  8. $appkey = " *********************";
  9. //************1. Query water quality in river basins************
  10. $url = "http://web.juhe.cn:8080/environment/water/river ";
  11. $params = array(
  12. "river" => "",//Basin name, if the query basin is "Yangtze River Basin", then 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. Monitoring site to check water quality********** **
  29. $url = "http://web.juhe.cn:8080/environment/water/state";
  30. $params = array(
  31. "state" => "",//Monitoring site name, query site If it is "Nanjinguan, Yichang, Hubei", then enter "Nanjinguan, Yichang, Hubei"
  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,//Application APPKEY
  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. * Request interface return Content
  67. * @param string $url [requested URL address]
  68. * @param string $params [requested parameters]
  69. * @param int $ipost [whether to use 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. }
Copy code
php, api


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template