您的位置:首页 > 移动开发

PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [2] 首页 APP 接口开发方案 ① 读取数据库方式

2015-03-21 20:02 1116 查看
方案一:读取数据库方式

从数据库读取信息→封装→生成接口数据

应用场景:

数据时效性比较高的系统

方案二:读取缓存方式

从数据库获取信息(第一次设置缓存或缓存失效时)→封装(第一次设置缓存或缓存失效时)→返回数据

↓ ↑

缓存(缓存生效时) → → → →

方案三:定时读取缓存方式(crontab 定时任务)

封装并返回数据



数据库→crontab→缓存



http 请求

=======

方案一:

(安装Start BlueStacks 安卓模拟器)

流程:

http 请求→服务器→查询数据(使用reviewdb库)→返回数据

修改一下 response.php line:29

if($type == 'json'){
self::json($code,$message,$data);
}else if($type == 'xml'){
self::xml($code,$message,$data);
}else if($type == 'array'){
var_dump($result);    //仅供测试
}


line 6:$data = ''

public static function show($code,$message = '',$data = '',$type = self::JSON){
.....


db.php:

<?php
/*
* 单例模式连接数据库
*/
class DB{
static private $_instance;    //非public的类的实例的静态成员变量
static private $_connectSource;    //连接数据库返回的资源句柄
private $_dbConfig = array(
'host'=>'127.0.0.1',
'username'=>'root',
'pwd'=>'',
'database'=>'reviewdb'
);

private function __construct(){    //非public 的构造函数
}

static public function getInstance(){    //访问实例的公共静态方法
if(!self::$_instance instanceof self){
self::$_instance = new self();
}
return self::$_instance;
}

public function connect(){
if(!self::$_connectSource){
//连接mysql服务
self::$_connectSource = @mysql_connect($this->_dbConfig['host'],$this->_dbConfig['username'],$this->_dbConfig['pwd']);
if(!self::$_connectSource){
//抛出异常
throw new Exception('mysql connect error'.mysql_error());
}
//选择数据库
mysql_select_db($this->_dbConfig['database'],self::$_connectSource);
//设置字符集
mysql_query('set names "UTF8"',self::$_connectSource);
}
return self::$_connectSource; //返回资源
}
}


list.php

<?php
require_once 'response.php';
require_once 'db.php';

$page = isset($_GET['page'])?$_GET['page']:1;
$pageSize = isset($_GET['pageSize'])?$_GET['pageSize']:1;
if(!is_numeric($page) || !is_numeric($pageSize)){
return @Response::show(401,'数据不合法');
}

$offset = ($page-1)*$pageSize; //每页起始数
$sql = 'select * from review where is_enabled = 1 order by creation_time desc limit '.$offset.','.$pageSize;

#捕获异常
try{
$connect = DB::getInstance()->connect();
}catch(Exception $e){
return Response::show(403,'数据库连接失败');
}

$res = mysql_query($sql,$connect);
$vals = array();
while($val = mysql_fetch_assoc($res)){
$vals[] = $val; //二维数组
}

if($vals){
return Response::show(200,'首页数据获取成功',$vals);
}else{
return Response::show(400,'首页数据获取失败',$vals);
}


附 response.php

 <?php

class Response{
const JSON = 'json';
//封装的综合方法,默认的数据类型为json
public static function show($code,$message = '',$data = '',$type = self::JSON){

if(!is_numeric($code)){
return '';
}
//供测试数组使用
$result = array(
'code' => $code,
'message' => $message,
'data' => $data
);
//通过get参数判断通信数据类型
$typelist = array('json','xml','array'); // array为测试使用
if(isset($_GET['type'])){
if(in_array(strtolower($_GET['type']),$typelist)){
$type = strtolower($_GET['type']);
}else{
$type = self::JSON;
}
}else{
$type = self::JSON;
}

if($type == 'json'){ self::json($code,$message,$data); }else if($type == 'xml'){ self::xml($code,$message,$data); }else if($type == 'array'){ var_dump($result); //仅供测试 }
}

/**
* 按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 xml($code,$message,$data){

if(!is_numeric($code)){
return '';
}

$result = array(
'code' => $code,
'message' => $message,
'data' => $data
);

//修改 http 头信息
header("Content-Type:text/xml");
//xml头信息
$xml = "<?xml version='1.0' encoding='utf-8'?>";
//根节点开始标签
$xml .= "<root>";

$xml .= self::xmlToEncode($result);

//根节点结束标签
$xml .= "</root>";

echo $xml;
exit();
}

//解析$result至xml
public static function xmlToEncode($data){
$xml = $attr = "";
foreach($data as $k=>$v){
//如果$k是数字(data(code,message,data中的data)数据里面还含有索引数组),要进行如下判断
if(is_numeric($k)){
$attr = "id='{$k}'";
$k = 'item ';
}

$xml .= "<{$k}{$attr}>";
//如果$v是数组,则递归调用该方法
if(is_array($v)){
$xml .= self::xmlToEncode($v);
}else{
$xml .= $v;
}
$xml .= "</{$k}>";
}

return $xml;
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐