PHP返回JSON和XML类,返回jsonxml_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP返回JSON和XML类,返回jsonxml_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2628字,纯文字阅读大概需要4分钟。
内容图文
PHP返回JSON和XML类,返回jsonxml
1 php 2 class Reponse{ 3 //private $result = array('code'=null,'message'=null,'data'=>null); 4 /** 5 * @desc 返回JSON格式 6 * @param int $code 7 * @param string $message 8 * @param array $data 9 * return string 10 */ 11 public static function json($code,$message = null,$data = array()){ 12 if(!is_numeric($code)){ 13 return false; 14 } 15 $result = array( 16 'code'=>$code, 17 'message'=>$message, 18 'data'=>$data 19 ); 20 return json_encode($result); 21 exit; 22 } 23 24 /** 25 * @desc 返回xml格式数据 26 * @parma int $code 状态码 27 * @param string $message 提示 28 * @param array $data 数据 29 * return string 30 */ 31 public static function xml($code,$message = '',$data = array()){ 32 if(!is_numeric($code)){ 33 return false; 34 } 35 $result = array( 36 'code'=>$code, 37 'message'=>$message, 38 'data'=>$data 39 ); 40 $xml = ''; 41 42 $xml .= "\n"; 43 $xml .= "\n"; 44 $xml .= self::xmlEncode($result); 45 $xml .= " "; 46 header("Content-Type:text/xml"); 47 echo $xml; 48 49 } 50 51 public static function xmlEncode($result){ 52 $xml = $attr =''; 53 foreach($result as $key=>$val){ 54 if(is_numeric($key)){ 55 $attr = "id='{$key}'"; 56 $key = "item{$key}"; 57 } 58 $xml .= "<{$key} {$attr}>"; 59 60 $xml .= is_array($val) ? self::xmlEncode($val) : $val; 61 62 $xml .= "</{$key}>\n"; 63 } 64 65 return $xml; 66 67 } 68 69 } 70 71 $data = array( 72 'id'=>1, 73 'age'=>20, 74 'username'=>'tim', 75 'others'=>array(1,2,3), 76 ); 77 Reponse::xml(200,'success',$data); 78 Reponse::json(200,'success',$data);
http://www.bkjia.com/PHPjc/948636.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/948636.htmlTechArticlePHP返回JSON和XML类,返回jsonxml 1 ? php 2 class Reponse{ 3 // private $result = array('code'=null,'message'=null,'data'=null); 4 /* * 5 * @desc 返回JSON格式 6 * @param i...
内容总结
以上是互联网集市为您收集整理的PHP返回JSON和XML类,返回jsonxml_PHP教程全部内容,希望文章能够帮你解决PHP返回JSON和XML类,返回jsonxml_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。