Interface Development Response Class encapsulation
classresponse{/** Packet Communication interface data * @param integer $code Status code * @param string $message status information * @param array $data data * Return St Ring*/ Public Static functionApi_response ($code,$message= ",$data=Array()){ //Logging error Logs if(1!==$code) { $logger=Newlogger (); $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; } } /** Package number is JSON data type * @param integer $code Status code * @param string $message status information * @param array $data data * Retu RN String*/ Public Static functionJson_response ($code,$message= ",$data=Array()){ $result= Self::grant_array ($code,$message,$data); EchoJson_encode ($result); Exit; } /** Package number is XML data type * @param integer $code Status code * @param string $message status information * @param array $data data * retur N String*/ Public Static functionXml_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; } /** Package number is an array data type * @param integer $code Status code * @param string $message status information * @param array $data data * RET Urn String*/ Public Static functionArray_response ($code,$message= ",$data=Array()){ $result= Self::grant_array ($code,$message,$data); Echo Var_export($result,true); Exit; } /** Convert array to XML format * @param array $array arrays * return string*/ Private Static functionXml_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; } /** Generate the original data array according to the interface format * @param integer $code Status code * @param string $message status information * @param array $data data * RET Urn Array*/ Private Static functionGrant_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; }}
Log class Encapsulation
classlogger{/** * Configuration*/ Public $conf=Array( ' Log_file ' = ', ' separator ' and ', ' ); /** * File handle*/ Private $fileHandler; /** * Get file Handle * @return Resource Filehandler*/ protected functionGetfilehandler () {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; } /** * Record log * @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 ()); }}
From: http://bbs.php-z.com/thread-3101-1-1.html
API Response Class