欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

基于php的水质查询api调用代码实例

程序员文章站 2022-04-14 09:59:17
...
代码描述:基于php的水质查询api调用代码实例
关联数据:水质量
接口地址:http://www.juhe.cn/docs/api/id/34
  1. // +----------------------------------------------------------------------
  2. //----------------------------------
  3. // 水质量调用示例代码 - 聚合数据
  4. // 在线接口文档:http://www.juhe.cn/docs/34
  5. //----------------------------------
  6. header('Content-type:text/html;charset=utf-8');
  7. //配置您申请的appkey
  8. $appkey = "*********************";
  9. //************1.流域查询水质量************
  10. $url = "http://web.juhe.cn:8080/environment/water/river";
  11. $params = array(
  12. "river" => "",//流域名称,查询流域为“长江流域”,则输入“长江流域”
  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 "请求失败";
  26. }
  27. //**************************************************
  28. //************2.监测站点查询水质量************
  29. $url = "http://web.juhe.cn:8080/environment/water/state";
  30. $params = array(
  31. "state" => "",//监测站点名称,查询站点为“湖北宜昌南津关”,则输入“湖北宜昌南津关”
  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 "请求失败";
  45. }
  46. //**************************************************
  47. //************3.监测站点列表************
  48. $url = "http://web.juhe.cn:8080/environment/water/stateList";
  49. $params = array(
  50. "key" => $appkey,//应用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 "请求失败";
  63. }
  64. //**************************************************
  65. /**
  66. * 请求接口返回内容
  67. * @param string $url [请求的URL地址]
  68. * @param string $params [请求的参数]
  69. * @param int $ipost [是否采用POST形式]
  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