Home > Backend Development > PHP Problem > How to check water quality in php

How to check water quality in php

藏色散人
Release: 2023-03-17 09:38:01
Original
1159 people have browsed it

How to query water quality in php: 1. Register and open the water quality API interface; 2. Configure the applied appkey; 3. Request the URL; 4. Get the interface parameters; 5. Through "function juhecurl($url, $params=false,$ispost=0){...}" and other methods can be used to analyze and process the water quality interface data.

How to check water quality in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to check water quality in php?

1. Open the water quality API interface:

viahttps://www.juhe.cn/docs/api/id/34?s=cpphpcnRegistration and activation

Interface description: This interface supports water quality queries at most monitoring sites across the country, including pH value, dissolved oxygen, ammonia nitrogen, potassium permanganate index and total organic carbon content, etc. Some monitoring sites do not have total organic carbon monitoring data.

Interface address: http://web.juhe.cn:8080/environment/water/river

Return format: json

Request method: get

Request example: http://web.juhe.cn:8080/environment/water/river?river=watershed name&key=APPKEY value you applied for

2. PHP-based water quality query api calling code Example

The code is as follows:

header('Content-type:text/html;charset=utf-8');
//配置您申请的appkey
$appkey = "*********************";
//************1.流域查询水质量************
$url = "http://web.juhe.cn:8080/environment/water/river";
$params = array(
"river" => "",//流域名称,查询流域为“长江流域”,则输入“长江流域”
"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 "请求失败";
}
//**************************************************
//************2.监测站点查询水质量************
$url = "http://web.juhe.cn:8080/environment/water/state";
$params = array(
"state" => "",//监测站点名称,查询站点为“湖北宜昌南津关”,则输入“湖北宜昌南津关”
"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 "请求失败";
}
//**************************************************
//************3.监测站点列表************
$url = "http://web.juhe.cn:8080/environment/water/stateList";
$params = array(
"key" => $appkey,//应用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 "请求失败";
}
//**************************************************
/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @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 after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to check water quality in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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