為什么我不能用header xml格式啊 可以查看源代碼 但是頁面不顯示
<?php
header("content-type:text/html;charset=utf-8");
class Response{
/**
*按json方式輸出通信數據
*@param integer $code狀態碼
*@param string $message 提示信息
*@param array $data 返回數據
*return string
**/
?? ?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;
?? ?}
/**
*按xml方式輸出通信數據
*@param integer $code狀態碼
*@param string $message 提示信息
*@param array $data 返回數據
*return string
**/
?? ?public static function xmlEncode($code,$message,$data = array()){
?? ??? ?if(!is_numeric($code)){
?? ??? ??? ?return '';
?? ??? ?}
?? ??? ?$result = array(
?? ??? ??? ?'code'=>$code,
?? ??? ??? ?'message'=>$message,
?? ??? ??? ?'data'=>$data
?? ??? ??? ?);
?? ??? ?$xml = "<?xml version='1.0 encoding='UTF-8?>\n";
?? ??? ?$xml.="<root>\n";
?? ??? ?$xml.=self::xmlToEncode($result);
?? ??? ?$xml.="</root>";
?? ??? ?echo $xml;
?? ?}
?? ?public static function xmlToEncode($data){
?? ??? ?$xml = "";
?? ??? ?$attr = "";
?? ??? ?foreach($data as $key => $value){
?? ??? ??? ?if(is_numeric($key)){
?? ??? ??? ??? ?$attr = " id='{$key}'";
?? ??? ??? ??? ?$key = "item";
?? ??? ??? ?}
?? ??? ??? ?$xml.="<{$key}{$attr}>";
?? ??? ??? ?$xml.=is_array($value)?self::xmlToEncode($value):$value;
?? ??? ??? ?$xml.="</{$key}>\n";
?? ??? ?}
?? ??? ?return $xml;
?? ?}
}
$data = array(
?? ?'id'=>1,
?? ?'name'=>'帥哥',
?? ?'type'=>array(2,3,5)
?? ?);
Response::xmlEncode(200,'success',$data);
?>
報錯如下
This page contains the following errors:
error on line 1 at column 19: String not closed expecting " or '
Below is a rendering of the page up to the first error.
2016-10-27
我的也是這個問題 ? 必須得查看源代碼才可以看到 ?頁面直接瀏覽報錯
2018-06-09
("content-type:text/html;charset=utf-8")??? test/html? charset 中間有個分號? 去掉就行了? 而且你是通過xml格式??? 所以要改成test/xml?
2016-10-09
你寫的header("content-type:text/html;charset=utf-8"); ? 中的content-type ? ?應該是Content-type或者Content-Type吧,是不是大小寫的問題