<?php /** * 设置页面输出的CONTENT_TYPE和编码 * @access public * @param string $type content_type 类型对应的扩展名 * @param string $charset 页面输出编码 * @return void */ function setContentType($type, $charset = '') { if (headers_sent()) return; if (empty($charset)) $charset = 'UTF-8'; $type = strtolower($type); // if (isset($this->_types[$type])) //过滤content_type // header('Content-Type: ' . $this->_types[$type] . '; charset=' . $charset); if (isset($type)) //过滤content_type header('Content-Type: ' . $type . '; charset=' . $charset); } /** * XML编码 * @param mixed $data 数据 * @param string $encoding 数据编码 * @param string $root 根节点名 * @return string */ function xml_encode($data, $encoding = 'utf-8', $root = 'think') { $xml = '<?xml version="1.0" encoding="' . $encoding . '"?>'; $xml .= '<' . $root . '>'; $xml .= data_to_xml($data); $xml .= '</' . $root . '>'; return $xml; } /** * 数据XML编码 * @param mixed $data 数据 * @return string */ function data_to_xml($data) { $xml = ''; foreach ($data as $key => $val) { is_numeric($key) && $key = "item id="$key""; $xml .= "<$key>"; $xml .= ( is_array($val) || is_object($val)) ? data_to_xml($val) : $val; list($key, ) = explode(' ', $key); $xml .= "</$key>"; } return $xml; } function dump($var, $echo = true, $label = null, $strict = true) { $label = ($label === null) ? '' : rtrim($label) . ' '; if (!$strict) { if (ini_get('html_errors')) { $output = print_r($var, true); $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>'; } else { $output = $label . print_r($var, true); } } else { ob_start(); var_dump($var); $output = ob_get_clean(); if (!extension_loaded('xdebug')) { $output = preg_replace('/]=> (s+)/m', '] => ', $output); $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>'; } } if ($echo) { echo($output); return null; } else return $output; } /** * 输出返回数据 * @access protected * @param mixed $data 要返回的数据 * @param String $type 返回类型 JSON XML * @param integer $code HTTP状态 * @return void */ function response($data, $type = '', $code = 200) { sendHttpStatus($code); exit(encodeData($data, strtolower($type))); } /** * 编码数据 * @access protected * @param mixed $data 要返回的数据 * @param String $type 返回类型 JSON XML * @return void */ function encodeData($data, $type = '') { if (empty($data)) return ''; if (empty($type)) $type = $this->_type; if ('json' == $type) { // 返回JSON数据格式到客户端 包含状态信息 $data = json_encode($data); } elseif ('xml' == $type) { // 返回xml格式数据 $data = xml_encode($data); } elseif ('php' == $type) { $data = serialize($data); }// 默认直接输出 setContentType($type); header('Content-Length: ' . strlen($data)); return $data; } // 发送Http状态信息 function sendHttpStatus($code) { static $_status = array( // Informational 1xx 100 => 'Continue', 101 => 'Switching Protocols', // Success 2xx 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', // Redirection 3xx 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Moved Temporarily ', // 1.1 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', // 306 is deprecated but reserved 307 => 'Temporary Redirect', // Client Error 4xx 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', // Server Error 5xx 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 509 => 'Bandwidth Limit Exceeded' ); if (isset($_status[$code])) { header('HTTP/1.1 ' . $code . ' ' . $_status[$code]); // 确保FastCGI模式下正常 header('Status:' . $code . ' ' . $_status[$code]); } } //使用方法,可能方法的顺序需要调整一下 $data = 11; response(array('code' => 1, 'msg' => '查询成功', 'data' => $data), 'json', 200);
2016年12月3日15:16:09 ,完全可用但是注意
if (isset($type)) //过滤content_type
header('Content-Type: ' . $type . '; charset=' . $charset);
这个可能会出现问题,so,自己在按需求改写下