'Not Found'); $message = 'Not Found'; } elseif ($code == 405) { $status = 405; $data = array('error' => 'Method not allowed'); $message = 'Method not allowed'; } else { $status = 200; $message = $msg; } //输出结果 header(self::HTTP_VERSION . " " . $status . " " . $message); $output = array( 'code' => (int)intval($code), 'message' => $message, ); $content_type = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : $_SERVER['HTTP_ACCEPT']; if (strpos($content_type, 'application/json') !== false) { if (is_array($data) && $data['page_config']) { $output = array_merge($output, $data); } else { $output['data'] = $data; } header("Content-Type: application/json"); echo self::encodeJson($output); die(); } else if (strpos($content_type, 'application/xml') !== false) { header("Content-Type: application/xml"); echo self::encodeXml($output); die(); } else { if (is_array($data) && $data['page_config']) { $output = array_merge($output, $data); } else { $output['data'] = $data; } header("Content-Type: application/json"); echo self::encodeJson($output); die(); } } //json格式 private static function encodeJson($responseData) { $str = json_encode($responseData); return $str; } //xml格式 private static function encodeXml($responseData) { $xml = new \SimpleXMLElement(''); foreach ($responseData as $key => $value) { if (is_array($value)) { foreach ($value as $k => $v) { $xml->addChild($k, $v); } } else { $xml->addChild($key, $value); } } return $xml->asXML(); } //html格式 private static function encodeHtml($responseData) { $html = ""; foreach ($responseData as $key => $value) { $html .= ""; if (is_array($value)) { foreach ($value as $k => $v) { $html .= ""; } } else { $html .= ""; } $html .= ""; } $html .= "
" . $k . "" . $v . "" . $key . "" . $value . "
"; return $html; } }