response.php //这个文件中封装了将数据转换为json 和xml数据的方法
<?php
header("content-type:text/html;charset=utf8");class Response{
const JSON="json";
/*
*按综合方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*@param string $type 数据类型
*return string
*/
public static function show($code,$message='',$data=array(),$type=self::JSON)
{
if(!is_numeric($code)){//判断是不是数字
return '';
}
$type = isset($_GET['format']) ? $_GET['format'] : self::JSON;
// echo $_GET['format'];
// die;
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
if($type == 'json'){
self::json($code,$message,$data);
exit;
}else if($type == 'array'){
var_dump($result);
}else if($type == 'xml'){
self::xmlIEncode($code,$message,$data);
exit;
}else{
//后续
}
}
/*
*按json方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*return string
*/
public static function json($code,$message='',$data=array()){
if(!is_numeric($code)){//判断是不是数字
return '';
}
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
echo json_encode($result);
exit;
}/*
*按xml方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*return string
*/
public static function xmlIEncode($code,$message,$data=array())
{
if(!is_numeric($code)){//判断是不是数字
return '';
}
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
header("content-type:text/xml");
$xml="<?xml version='1.0' encoding='UTF-8'?>\n";
$xml.="<root>\n";
$xml.=self::xmlToEncode($result);
$xml.="</root>";
echo $xml;
}
public static function xmlToEncode($data){
$xml = $attr = "";
foreach($data as $key => $value) {
if(is_numeric($key)) {
// $attr="id='{$key}'";
$attr=" id='{$key}'";
$key="item";
}
$xml.="<{$key}{$attr}>";
$xml.=is_array($value)?self::xmlToEncode($value):$value;
$xml.="</{$key}>\n";
}
return $xml;
}
}Db.php //连接数据库的文件
<?php
class Db{
static private $_instance; //存对象的属性
static private $_connectSource; //存连接数据库对象
private $_dbConfig=array(
'host'=>'127.0.0.1',
'user'=>'root',
'password'=>'root',
'database'=>'test'
);
private function __construct()
{}
static public function getInstance()
{
if(!(self::$_instance instanceof self)){
self::$_instance= new self();
}
return self::$_instance;
}public function connect()
{
if(!self::$_connectSource){
self::$_connectSource=mysql_connect($this->_dbConfig['host'],$this->_dbConfig['user'],$this->_dbConfig['password']);
if(!self::$_connectSource){
//如果数据库连接失败,就抛出异常
throw new Exception('mysql connect error'.mysql_error());
//die('mysql connect error'.mysql_error());
}
mysql_select_db($this->_dbConfig['database'],self::$_connectSource);
mysql_query("set names UTF8",self::$_connectSource);
}
return self::$_connectSource;
}}
// $connect=Db::getInstance()->connect();
// $sql="select * from city";
// $result=mysql_query($sql,$connect);
// var_dump($result);list 接口文件
<?php
//http://app.com/list.php?page=1&pagesize=12
require_once('./response.php');
require_once('./Db.php');
$page=isset($_GET['page'])?$_GET['page']:1;
$pageSize=isset($_GET['pageSize'])?$_GET['pageSize']:3;
if(!is_numeric($page) || !is_numeric($pageSize))
{
return Response::show(401,'数据不合法');
}
$offset=($page-1)*$pageSize;
$sql="select * from city where ProvinceCode=130000 limit ".$offset.",".$pageSize;try{
$connect=Db::getInstance()->connect();}catch(Exception $e){
return Response::show(403,'数据库连接失败');
}
$result=mysql_query($sql,$connect);
// var_dump($result);
while($video=mysql_fetch_assoc($result)){
$videos[]=$video;
};
//把videos[]转换成json数据
if($videos){
return Response::show(200,'首页数据获取成功',$videos);
}else{
return Response::show(400,'首页数据获取失败',$videos
);
}
代码地址 https://pan.baidu.com/s/1jqixphMYvlpArTv58NOpKg