码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-05-15 15:49:02      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:控制器   接口   信息   天气   

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‘),
    ),

);
?>









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

标签:控制器   接口   信息   天气   

原文地址:http://pilipala.blog.51cto.com/10165737/1651561

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!