search
Homephp教程php手册PHP实现返回JSON和XML的类分享,php返回jsonxml

PHP实现返回JSON和XML的类分享,php返回jsonxml

代码很简洁,功能也很简单实用,这里就不多废话了,直接奉上代码:

复制代码 代码如下:


    class Reponse{
        //private $result = array('code'=null,'message'=null,'data'=>null);
        /**
         * @desc 返回JSON格式
         * @param int $code
         * @param string $message
         * @param array  $data
         * return string
         */
        public static function json($code,$message = null,$data = array()){
            if(!is_numeric($code)){
                return false;
            }
            $result = array(
                'code'=>$code,
                'message'=>$message,
                'data'=>$data
            );
            return json_encode($result);
            exit;
        }
        /**
         * @desc 返回xml格式数据
         * @parma int $code 状态码
         * @param string $message 提示
         * @param array $data 数据
         * return string
         */
         public static function xml($code,$message = '',$data = array()){
            if(!is_numeric($code)){
                return false;
            }
            $result = array(
                'code'=>$code,
                'message'=>$message,
                'data'=>$data
            );
            $xml = '';
            $xml .= "\n";
            $xml .= "\n";
            $xml .= self::xmlEncode($result);
            $xml .= "
";
            header("Content-Type:text/xml");
            echo $xml;
         }
         public static function xmlEncode($result){
            $xml = $attr ='';
            foreach($result as $key=>$val){
                if(is_numeric($key)){
                    $attr = "id='{$key}'";
                    $key = "item{$key}";
                }
                $xml .= "";
                $xml .= is_array($val) ? self::xmlEncode($val) : $val;
                $xml .= "{$key}>\n";
            }
            return $xml;
         }
    }
    $data = array(
        'id'=>1,
        'age'=>20,
        'username'=>'tim',
        'others'=>array(1,2,3),
    );
    Reponse::xml(200,'success',$data);
    Reponse::json(200,'success',$data);

小伙伴们可以直接拿去使用,使用方法在代码的最下方:)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment