[PHP实战]ThinkPHP入门-2 一个简单的PHP接口程序(mysql+json)

128 阅读1分钟

ThinkPHP入门-2

1.需要环境:

ThinkPHP入门-1
在这里插入图片描述配置Sql数据:
在这里插入图片描述

/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 50508
 Source Host           : localhost:3306
 Source Schema         : test

 Target Server Type    : MySQL
 Target Server Version : 50508
 File Encoding         : 65001

 Date: 14/03/2022 15:37:48
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'dong', 28);

SET FOREIGN_KEY_CHECKS = 1;

2.定义简单php接口

2.1复制Home到Index文件夹

在这里插入图片描述

2.2更改数据库

在这里插入图片描述

2.3更改接口访问

ThinkPHP/Conf/convention.php 更改内容

'DEFAULT_MODULE'        =>  'Index',  // 默认模块

Application/Index/Controller/IndexController.class.php 替换内容

<?php
namespace Index\Controller;
use Think\Controller;
class IndexController extends Controller {
    //TODO 根據條件更改
    // 'DEFAULT_MODULE'        =>  'Index',  // 默认模块

    // http://localhost/thinkphp/index.php/Index
    // http://localhost/thinkphp/index.php/Index/Index
    // http://localhost/thinkphp/index.php/Index/Index/Index
    public function index(){
        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>PHP-東寶軟體...</b>!</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    }

    // http://localhost/thinkphp/index.php/Index/index/mysqlTest
    public function mysqlTest(){
        $this->show("abc;<br>");
        $User = M("user")->Select();
        echo "mysql API Response:".$User[0]['name'];
    }

    // http://localhost/thinkphp/index.php/Index/index/jsonTest
    public function jsonTest(){
        header('Content-Type:application/json; charset=utf-8');
        $arr = array('a'=>1,'b'=>2);
        exit(json_encode($arr));
//        $this->ajaxReturn(json_encode($arr));
    }
}

在这里插入图片描述

3.测试改动

3.1 测试访问Index

http://localhost/thinkphp/index.php/Index/Index/Index
在这里插入图片描述

3.2 测试访问Mysql

http://localhost/thinkphp/index.php/Index/index/mysqlTest
在这里插入图片描述

3.3 测试访问Json

http://localhost/thinkphp/index.php/Index/index/jsonTest

在这里插入图片描述

4.项目地址

github地址

5.书接上文

ThinkPHP入门-1