php从网关接收json数据,保存数据库
将接收到数据保存到数据中
connDB.php
<?php
header("Content-type:text/html;charset=utf-8");
class DBHelper {
static $ini=null;
static $conn = null;
function __construct(){ #构造函数
$this->getIni();
$this->connect();
}
function __destruct(){ #析构函数
$this->close();
}
private function getIni(){ #获取配置文件数据
include './conf.php';
self::$ini = $ini;
}
private function connect(){ #连接
if (self::$conn != null){
return self::$conn;
}
try{
self::$conn = new PDO(self::$ini['MYSQL_URL'],self::$ini['MYSQL_USER'],self::$ini['MYSQL_PASSWD']);
self::$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
self::$conn->exec('set names utf8');
$this->createDB();
$this->createTable();
}
catch (PDOException $e){
echo "Connection fialed:".$e->getMessage();
}
return self::$conn; #返回的值也可能是为null,在使用前需要判断下
}
/*
function getConn(){ #返回数据库连接
if (self::$conn == null){
connect();
}
return self::$conn;
}
*/
private function createDB(){ #当数据库不存在时候创建数据库
if (self::$conn == null){
$this->connect();
}
// print_r(self::$ini);
$sql = 'CREATE DATABASE IF NOT EXISTS '.self::$ini['MYSQL_DBNAME'].' CHARACTER SET UTF8;';
// echo $sql;
self::$conn->exec($sql);
self::$conn->exec('USE '.self::$ini['MYSQL_DBNAME'].';');
}
private function createTable(){
if (self::$conn == null){
connect();
}
$sql = 'CREATE TABLE IF NOT EXISTS '.self::$ini['MYSQL_TABLE'].'('.
'id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,'.
'temp INT,'.
'humi INT,'.
'photo INT,'.
'air INT);';
// echo $sql;
self::$conn->exec($sql);
}
public function insertJsonData($json){
if(self::$conn==null){
$this->connect();
}
$sql = 'INSERT INTO '.self::$ini['MYSQL_TABLE'].' (`temp`,`humi`,`photo`,`air`) VALUES (:json);';
$stmt = self::$conn->prepare($sql);
$stmt->bindParam(':json',$json);
return $stmt->execute();
}
public function insertOneData($sData){ #插入一条数据
if(self::$conn==null){
$this->connect();
}
$sql = 'INSERT INTO '.self::$ini['MYSQL_TABLE'].' (temp,humi,photo,air) VALUES (:temp,:humi,:photo,:air);';
$stmt = self::$conn->prepare($sql); #使用预处理插入数据
// $stmt->bindParam(':temp',$sData->temp);
// $stmt->bindParam(':humi',$sData->humi);
// $stmt->bindParam(':photo',$sData->photo);
// $stmt->bindParam(':air',$sData->air);
$stmt->bindParam(':temp',$temp);
$stmt->bindParam(':humi',$humi);
$stmt->bindParam(':photo',$photo);
$stmt->bindParam(':air',$air);
$temp = $sData->temp;
$humi = $sData->humi;
$photo = $sData->photo;
$air = $sData->air;
return $stmt->execute(); #成功返回true 失败返回false
}
public function insertArrData($sArrData){ #插入多条数据,存储在sensorData类型数组中
$len = count($sArrData);
for ($i=0;$i<$len;$i=$i+1){
$this->insertOneData($sArrData[$i]);
}
}
######################## 从0开始 偏移量
public function queryData($start,$offset){ #查询指定条数数据
if(self::$conn==null){
$this->connect();
}
$sql = "SELECT * FROM ".self::$ini['MYSQL_TABLE']." ORDER BY id LIMIT $start,$offset;";
// $sql = 'SELECT * FROM '.self::$ini['MYSQL_TABLE'].' WHERE id BETWEEN ? and ?';
$stmt = self::$conn->prepare($sql);
$arr_data = array();
###################array($start,$offset)
//if ($stmt->execute(array($start,$end)) ){
if ($stmt->execute()){
$sensorData = new SensorData();
while ( $row = $stmt->fetch()){
$sensorData->temp = $row['temp'];
$sensorData->humi = $row['humi'];
$sensorData->photo = $row['photo'];
$sensorData->air = $row['air'];
array_push($arr_data,$sensorData);
}
}
return $arr_data;
}
public function queryAllData(){ #查询所有数数据
if(self::$conn==null){
$this->connect();
}
$sql = "SELECT * FROM ".self::$ini['MYSQL_TABLE'];
$stmt = self::$conn->prepare($sql);
if ($stmt->execute()){
$sensorData = new SensorData();
while ( $row = $stmt->fetch()){
$sensorData->temp = $row['temp'];
$sensorData->humi = $row['humi'];
$sensorData->photo = $row['photo'];
$sensorData->air = $row['air'];
array_push($arr_data,$sensorData);
}
}
return $arr_data;
}
public function deleteOneData(){
if(self::$conn==null){
$this->connect();
}
}
public function deleteArrData(){
if(self::$conn==null){
$this->connect();
}
}
public function updataOneData(){
if(self::$conn==null){
$this->connect();
}
}
public function updataArrData(){
if(self::$conn==null){
$this->connect();
}
}
public function close(){ #关闭数据库连接
if(self::$conn!=null){
self::$conn = null;
}
}
}
##############################################
class SensorData{
private $id = null;
private $temp = null;
private $humi = null;
private $photo = null;
private $air = null;
function __construct(){}
function __destruct(){}
public function __get($property_name){ #获得属性值
if (isset($this->$property_name)){
return $this->$property_name;
}
return null;
}
public function __set($property_name,$value){ #设置属性值
$this->$property_name = $value;
}
public function __toString(){ #打印信息,调试
return '<br/>data:<br/>temp:'.$this->temp.'<br/>humi:'.$this->humi.'<br/>photo:'.$this->photo.'<br/>air:'.$this->air;
}
}
##############################################
/*
$test = new DBHelper();
$json_data = '{"air":17,"humi":46,"photo":46,"temp":27}';
$arr_data = json_decode($json_data,true);
print_r($arr_data);
$sData = new SensorData();
$sData->temp = $arr_data['temp'];
$sData->humi = $arr_data['humi'];
$sData->photo = $arr_data['photo'];
$sData->air = $arr_data['air'];
//echo 'suydg'.$sData->air;
echo $sData;
echo '<br/>';
*/
####接收post数据
//$input_json = file_get_contents("php://input",true); #接收post数据
//$arr_data = json_encode($input_json);
/*
$json_data = '{"air":17,"humi":46,"photo":46,"temp":27}';
$test = new DBHelper();
$data1 = json_decode($json_data); #转成类类型
$data = get_object_vars($data1);
print_r($data);
$sData = new SensorData();
$sData->temp = $data['temp'];
$sData->humi = $data['humi'];
$sData->photo = $data['photo'];
$sData->air = $data['air'];
echo $sData;
*/
#echo $test->insertOneData($sData);
echo "<br/>";
$postData = file_get_contents("php://input");
echo "=====\$_POST=======<br/>";
echo var_dump($_POST)."<br/>";
echo "=====php://input===<br/>";
echo $postData."<br/>";
echo isset($postData).'sjtr<br/>';
echo var_dump($_GET).'<br/>';
/*
if(isset($postData)){
echo 'hello';
$request = json_decode($postData);
print_r($request);
$sData->temp = $request->temp;
$sData->humi = $request->humi;
$sData->photo = $request->photo;
$sData->air = $request->air;
echo $test->insertOneData($sData);
}
wlecome <?php
$i=0;
print_r($_POST);
$raw_data = file_get_contents('php://input');
if(!file_exists('test2.html')){
file_put_contents('test2.html','2.html<br/>');
}
print_r($raw_data);
while($raw_data){
file_put_contents('test2.html',$raw_data,FILE_APPEND);//,FILE_APPEND);
file_put_contents('test2.html','<br/>',FILE_APPEND);
print_r($raw_data);
$i=$i+1;
if($i==10){
file_put_contents('test2.html','');
}
}
?>.<br/>
*/
?>
读取配置文件
conf.php
<?php
header("Content-type:text/html;charset=utf-8");
/*
class Configure{
static function getConf(){
$ini = null;
$file = './conf.ini';
if (file_exists($file)){
$ini = parse_ini_file($file);
}
return $ini;
}
}
*/
$ini = null;
$file = './conf.ini';
if (file_exists($file)){
$ini = parse_ini_file($file);
}
?>
配置文件:
conf.ini
####MySQL配置参数 '123456'
########################## ip port
MYSQL_URL = 'mysql:host=localhost;port=3306;'
MYSQL_USER = 'root'
MYSQL_PASSWD = ''
MYSQL_DBNAME = 'test'
MYSQL_TABLE = 'sensor_data'
网关程序
http://blog.csdn.net/shawshank_bingo/article/details/73005070