Home > Backend Development > PHP Tutorial > PHP development APP interface method

PHP development APP interface method

墨辰丷
Release: 2023-03-25 19:26:02
Original
4286 people have browsed it

使用PHP来生成APP接口数据是非常简单的,如果你还不了解PHP没有关系,只需要看过PHP的基本语法,再看本示例就可以了。 

APP接口一般都是json格式(当然也有少数xml格式)遵循restful规范的:

    {
        code:200,
        msg:"这个是提示数据",
        data:[这里是数组数据]
    }
Copy after login

为了能立竿见影先看到效果,这里贴一个最简单最简单的一个demo代码
后面再贴一个基本完整的demo代码,带缓存和数据库查询的。先看简单的:
两个文件:一个封装了生成json的方法,一个是请求接口文件:

Response.php

<?php

 class Response{
    /**
     * @param $code 状态码
     * @param string $message  提示信息
     * @param array $data 数据
     * return string
     * 按json方式输出通信数据
     */
    public static function json($code,$message=&#39;&#39;,$data=array()){
        if(!is_numeric($code)){
            return &#39;&#39;;
        }
        $result = array(
          &#39;code&#39;=>$code,
          'message'=>$message,
            'data'=>$data
        );
        echo json_encode($result);
        exit;
    }
}
Copy after login

test.php

<?php
require_once(&#39;./Response.php&#39;);
$arr = array(
    &#39;id&#39; => 1,
    'name'=>'singwa'

);
Response::json(200,"数据返回成功",$arr);
Copy after login

两个文件,就完成了一个最简单的请求接口demo,现在测试一下,我本地安装了个PHPstudy,将这两个文件放在www根目录里,浏览器输入接口地址

http://localhost/test.php
Copy after login

相关推荐:

PHP实现批量生成App各种尺寸Logo,phpapp尺寸logo_PHP教程

解决ThinkPHP开启APP_DEBUG=>false时报错的问题,thinkphpapp_debug_PHP教程

【关于php】Appserv中关于DW配置站点问题,phpappservdw站点_PHP教程


The above is the detailed content of PHP development APP interface method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template