<?php
require_once "./response.php";
require_once "./db.php";
require_once "./file.php";
$page = isset($_GET['page'])?$_GET['page']:1;
$pagesize = isset($_GET['pagesize'])?$_GET['pagesize']:10;
if(!is_numeric($page) || !is_numeric($pagesize)){
return Response::show(401,'数据不合法');
}
$cache = new File;
$result = array();
if(!$result = $cache->cacheData('test'.$page.'_'.$pagesize)){
$offset = ($page-1)*$pagesize;
$sql = "select * from sys_role order by 'name' desc limit {$offset},{$pagesize}";
try{
$db = Db::getInstance()->connect();
}catch (EXception $e){
return Response::show(403,'数据库连接失败');
}
$recordset = $db->query($sql);
$recordset->setFetchMode(PDO::FETCH_ASSOC);
$result = $recordset->fetchAll();
if($result){
$cache->cacheData('test'.$page.'_'.$pagesize,$result,1200);
}
}
if($result){
return Response::show(200,'首页数据获取成功',$result);
}else{
return Response::show(400,'首页数据获取失败');
}
シングルトンクラスファイルデータベースパッケージ導入をdb.php
<?php
class Db
{
static private $_instance;
static private $_connectSource;
const HOST='mysql:host=localhost;dbname=rht-test';
const USER='root';
const PASS='root';
private function __construct()
{
}
public static function getInstance()
{
if(!(self::$_instance instanceof self)){
self::$_instance = new self();
}
return self::$_instance;
}
private function __clone()
{
trigger_error("Can't clone object",E_USER_ERROR);
}
public function connect()
{
self::$_connectSource = new PDO(Db::HOST,Db::USER,Db::PASS);
if(!self::$_connectSource){
throw new Exception("mysql connect error");
}
self::$_connectSource->exec("set names utf8");
return self::$_connectSource;
}
}
タイミング静的ファイル生成されたクラスファイルのfile.phpの紹介
<?php
class File
{
private $_dir;
const EXT = '.txt';
public function __construct()
{
$this->_dir = dirname(__FILE__).'\files\/';
}
public function cacheData($path='',$value='',$cacheTime=0)
{
$filename = $this->_dir.$path.self::EXT;
if($value !==''){
if(is_null($value)){
return @unlink($filename);
}
$dir = dirname($filename);
if(!is_dir($dir)){
mkdir($dir,0777);
}
$cacheTime = sprintf('%011d',$cacheTime);
return file_put_contents($filename,$cacheTime.json_encode($value));
}
if(!is_file($filename)){
return false;
}else{
$connects = file_get_contents($filename);
$cacheTime = substr($connects,0,11);
$value = substr($connects,11);
if($cacheTime !=0 && $cacheTime+fileatime($filename) < time()){
unlink($filename);
return false;
}
return json_decode($value,true);
}
}
}
パッケージのクラスファイルの導入を持つXML、JSON通信フォーマットresponse.php
<?php
class Response
{
const TYPE = 'json';
public static function show($code,$message='',$data=array(),$type=self::TYPE){
if(!is_numeric($code)){
return "";
}
$type = isset($_GET['format']) ? $_GET['format'] : self::TYPE;
$result = array(
'code'=>$code,
'message'=>$message,
'data'>$data
);
if($type == 'json'){
self::json($code,$message,$data);
exit;
}elseif($type == 'xml'){
self::xmlEncode($code,$message,$data);
exit;
}elseif($type == 'array'){
print_r($result);exit;
}else{
}
}
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;
}
public static function xml()
{
header("Content-type:text/xml");
$xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
$xml .= "<root>\n";
$xml .= "<code>200</code>\n";
$xml .= "<message>数据返回成功</message>\n";
$xml .= "<data>\n";
$xml .= "<id>1</id>\n";
$xml .= "<name>caicai</name>\n";
$xml .= "<desc>this is a test</desc>\n";
$xml .= "</data>\n";
$xml .= "</root>";
echo $xml;
exit;
}
public static function xmlEncode($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>";
$xml .= self::xmlToEncode($result);
$xml .= "</root>";
echo $xml;exit;
}
public static function xmlToEncode($data){
$xml = $attr = "";
foreach($data as $key=>$val){
if(is_numeric($key)){
$attr = "id= '{$key}'";
$key = "item ";
}
$xml .= "<{$key} {$attr}>";
$xml .= is_array($val) ? self::xmlToEncode($val) : $val;
$xml .= "</{$key}>\n";
}
return $xml;
}
}
$data = array(
'id'=>1,
'name'=>'caicai',
);