温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Tp3.2 RESTFul 根据地区查询生成xml、html、json的Api接口的天气信息

发布时间:2020-07-13 12:27:03 来源:网络 阅读:529 作者:噼里啪啦啦 栏目:软件技术

1、在控制器页面:

<?php
// 本类由系统自动生成,仅供测试用途
namespace Home\Controller;
use Think\Controller\RestController;
class IndexController extends RestController{
    //显示页面
    public function index(){
       //print_r($this->_method);die;
        $Name = $_GET['data'];
        if(!preg_match("/^[A-Za-z]+$/",$Name)){
            echo "输入有误";die;
        }
        if(__EXT__ == ''){
            $type="html";
        }else{
            $type=__EXT__;
        }
        switch ($this->_method){   
            case 'get': // get请求处理代码          
            if (__EXT__ == 'html'){
                $html = $this->sel($Name);
                $this->response($html,'html');
                //echo "html";
            }elseif(__EXT__  == 'xml'){ 
                $xml = $this->sel($Name);
                $this->response($xml,'xml');
                
            }elseif(__EXT__  == 'json'){
                $json = $this->sel($Name);
                $this->response($json,'json');
            }           
            break;
            case 'put': // put请求处理代码          
            break;      
            case 'post': // post请求处理代码           
            break;     
        }   
    }
    public function sel($name){
        //print_r($name);die;
        $sheng = M('sheng');  //实例化表
        $city = M('city');
        $xian = M('xian');
        if($name=="china"){
            $data=$sheng->select();
            return $data;
        }else{
            $s_id =$sheng->getfieldbypyname($name,'id');  //获取查询省id
            //print_r($s_id);die;
            //判断获取省id是否为空,如果不为空查询获取省的下级的关联id的相关数据
            if(!$s_id==""){
                $c_id['p_id']=$s_id;    
                $datas = $city->where($c_id)->select(); 
                //print_r($datas);die;
                return $datas;
            }else{
                $city_id =$city->getfieldbypyname($name,'id'); //获取市id  getFieldBy 根据字段查询并返回某个字段的值 例如,getFieldByName 
                //print_r($city_id);die;
                if(!$city_id==""){
                    $x_id['p_id']=$city_id;
                    $data1 = $xian->where($x_id)->select();
                    //print_r($data1);
                    return $data1;
                }else{
                    $xian_id = $city_id =$xian->getfieldbypyname($name,'id');
                    //print_r($xian_id);die;
                    if(!$xian_id==""){
                        $cx_id['id']=$xian_id;
                        //print_r($cx_id);die;
                        $data2 = $xian->where($cx_id)->select();
                        //print_r($data2);die;
                        return $data2;
                    }else{
                        echo "没有该地区!";
                    }
                }
            }
        }
    }
}

2、配置文件:

<?php
return array(
    //'配置项'=>'配置值'
/* 数据库设置 */
    'DB_TYPE'               => 'mysql',     // 数据库类型
    'DB_HOST'               => '127.0.0.1', // 服务器地址
    'DB_NAME'               => 'news',          // 数据库名
    'DB_USER'               => 'root',      // 用户名
    'DB_PWD'                => '',          // 密码
    'DB_PORT'               => '3306',        // 端口

    'URL_ROUTER_ON'   => true, //开启路由
    'URL_ROUTE_RULES'=>array(    
         'Index/:data' => array('Index/index', 'status=1'),
    ),

);
?>









向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI