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

1、在控制器页面:

成都创新互联-成都网站建设公司,专注成都做网站、网站制作、网站营销推广,国际域名空间,雅安服务器托管,网站托管、服务器租用有关企业网站制作方案、改版、费用等问题,请联系成都创新互联

_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、配置文件:

'配置值'
/* 数据库设置 */
    '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'),
    ),

);
?>


新闻名称:Tp3.2RESTFul根据地区查询生成xml、html、json的Api接口的天气信息
网页URL:http://ybzwz.com/article/gppied.html