接口开发响应类封装
class response{
/*
* 封通信接口数据
* @param integer $code 状态码
* @param string $message 状态信息
* @param array $data 数据
* return string
*/
public static function api_response($code, $message='', $data=array()){
//记录错误日志
if (1 !== $code) {
$logger = new logger();
$logger->conf['log_file'] = _LOG_DIR_ . 'api_error' . '.log';
$logger->log(array(substr(getIp(), 0, 15), '['.date("Y-m-d H:i:s").']', 'code: '.$code, 'message: '.$message));
} $type = isset($_REQUEST['format'])?$_REQUEST['format']:'';
switch ($type) {
case 'json':
self::json_response($code, $message, $data);
break;
case 'xml':
self::xml_response($code, $message, $data);
break;
case 'array':
self::array_response($code, $message, $data);
break;
default:
self::json_response($code, $message, $data);
break;
}
} /*
* 封装数为json数据类型
* @param integer $code 状态码
* @param string $message 状态信息
* @param array $data 数据
* return string
*/
public static function json_response($code, $message='', $data=array()){
$result = self::grant_array($code, $message, $data);
echo json_encode($result);
exit;
} /*
* 封装数为xml数据类型
* @param integer $code 状态码
* @param string $message 状态信息
* @param array $data 数据
* return string
*/
public static function xml_response($code, $message='', $data=array()){
$result = self::grant_array($code, $message, $data); header("Content-Type:text/xml");
$xml = "<?xml version='1.0' encoding='UTF-8'?>".PHP_EOL;
$xml .= "<root>".PHP_EOL;
$xml .= self::xml_encode($result);
$xml .= "</root>";
echo $xml;
exit;
} /*
* 封装数为array数据类型
* @param integer $code 状态码
* @param string $message 状态信息
* @param array $data 数据
* return string
*/
public static function array_response($code, $message='', $data=array()){
$result = self::grant_array($code, $message, $data);
echo var_export($result, true);
exit;
} /*
* 将数组转换为XML格式
* @param array $array 数组
* return string
*/
private static function xml_encode($array=array()){
$xml = $attr = ""; if(!empty($array)){
foreach ($array as $key => $value) {
if(is_numeric($key)){
$attr = " id='{$key}'";
$key = "item";
}
$xml .= "<{$key}{$attr}>" ;
$xml .= is_array($value) ? self::xml_encode($value) : $value;
$xml .="</{$key}>".PHP_EOL;
}
}
return $xml;
} /*
* 按照接口格式生成原数据数组
* @param integer $code 状态码
* @param string $message 状态信息
* @param array $data 数据
* return array
*/
private static function grant_array($code, $message='', $data=array()){
if(!is_numeric($code)){
return '';
}
$data['date'] = date('Y-m-d H:i:s');
$result = array(
'code' => $code,
'message' => $message,
'data' => $data
); return $result;
}
}
日志类封装
class logger
{
/**
* 配置
*/
public $conf = array(
'log_file' => '',
'separator' => ' ',
); /**
* 文件句柄
*/
private $fileHandler; /**
* 获得文件句柄
* @return resource fileHandler
*/
protected function getFileHandler()
{
if (null === $this->fileHandler)
{
if (empty($this->conf['log_file']))
{
trigger_error('no log file spcified');
}
$logDir = dirname($this->conf['log_file']);
if (!is_dir($logDir))
{
mkdir($logDir, 0777, true);
}
$this->fileHandler = fopen($this->conf['log_file'], 'a');
}
return $this->fileHandler;
} /**
* 记录日志
* @param mixed $logData
*/
public function log($logData)
{
if ('' == $logData || array() == $logData)
{
return false;
}
if (is_array($logData))
{
$logData = implode($this->conf["separator"], $logData);
}
$logData = $logData. "\n";
fwrite($this->getFileHandler(), $logData);
fclose($this->getFileHandler());
}
}
来自:http://bbs.php-z.com/thread-3101-1-1.html