博主信息
博文 40
粉丝 0
评论 0
访问量 53008
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
php开发app后台
无須終有的博客
原创
1930人浏览过
  1. php生成 JSON 数据

        方法json_encode($value);

        该函数只能接受utf-8编码的数据,如果传递其他格式的数据函数返回null

<?php          
        header("content_type:text/html;charset=utf8");
        $arr=array(
        'id'=>1,
        'name'=>'siangwa'
        );
        echo json_encode($arr);

2.封装一个返回json简单的方法

response.php

<?php  
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;
    }
}

test.php

<?php  
    header('content-type:text/html;charset=utf8');
    require_once('./response.php');
    $arr=array(
    'id'=>1,
    'name'=>'singwa'
);
Response::json(200,'数据返回成功',$arr);

结果:QQ截图20180717103705.png

/**********************************************************************************************************************/

3.php生成xml数据

   1>字符串组装

   2>使用系统类


4.写一个简单的xml数据方法

response.php

class Response{
    
    public static function xml()
    {
    header("content-type:text/xml;charset=utf8"); 
    $xml="<?xml version='1.0' encoding='UTF-8'?>\n";
    $xml.="<root>\n";
    $xml.="<code>200</code>\n";
    $xml.="<message>数据返回成功</message>\n"; 
    $xml.="<data>\n";
    $xml.="<id>1</id>\n";
    $xml.="<name>singwa</name>\n";
    $xml.="</data>\n";
    $xml.="</root>";
    echo $xml;
    }
}
Response::xml();

结果:

QQ截图20180717111108.png

/**********************************************************************************************************************/

5.xml方式封装接口数据方法

response.php

class Response{
/*
*按xml方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*return string
*/
public static function xmlIEncode($code,$message,$data=array())
    {
        if(!is_numeric($code)){//判断是不是数字
        return '';
        }
        $result=array(
        'code'=>$code,
        'message'=>$message,
        'data'=>$data
        );
        header("content-type:text/xml");
        $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'=>'towan',
    'type'=>array(1,23,4)
);
 Response::xmlIEncode(200,'success',$data);

结果:

QQ截图20180717121704.png

/**********************************************************************************************************************/

6.封装通信接口数据方法

response.php

<?php 
 header("content-type:text/html;charset=utf8"); 
class Response{
const JSON="json";
/*
*按综合方式输出通信数据
*@param integer $code 状态码
*@param string $message 提示信息
*@param array $data 数据
*@param string $type 数据类型
*return string
*/
public static function show($code,$message='',$data=array(),$type=self::JSON)
{
if(!is_numeric($code)){//判断是不是数字
return '';
}
$type = isset($_GET['format']) ? $_GET['format'] : self::JSON;
// echo $type;die;
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
if($type == 'json'){
self::json($code,$message,$data);
exit;
}else if($type == 'array'){
var_dump($result);
}else if($type == 'xml'){
self::xmlIEncode($code,$message,$data);
exit;
}else{
//后续
}
}
/*
*按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 xmlIEncode($code,$message,$data=array())
{
if(!is_numeric($code)){//判断是不是数字
return '';
}
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
header("content-type:text/xml");
$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}'";
$attr=" id='{$key}'";
$key="item";
}
$xml.="<{$key}{$attr}>";
$xml.=is_array($value)?self::xmlToEncode($value):$value;
$xml.="</{$key}>\n";
}
return $xml;
}
}

test.php

<?php  
require_once('./response.php');
$data=array(
'id'=>1,
'name'=>'singwa',
'type'=>array(4,5,6),
'test'=>array(1,23,45=>array(213,'asdfa'))
);
Response::show(200,'数据返回成功',$data);


/*************************************************************************************************************************/

代码下载

https://pan.baidu.com/s/16pbWruiKmtczXeikM_lNIg

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学