码迷,mamicode.com
首页 > Web开发 > 详细

ThinkPHP 中国天气接口事例

时间:2015-05-15 10:39:31      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:thinkphp   接口   天气   

编写路由:

‘URL_ROUTER_ON‘   => true,  //开启路由
‘URL_ROUTE_RULES‘=>array(
    ‘index/:weather‘=>‘Index/index‘,
),

控制器内:

<?php
namespace Home\Controller;
use Think\Controller\RestController;
class IndexController extends RestController{

    Public function Index(){
        $name=$_GET[‘weather‘];
        if(!preg_match("/^[a-zA-Z]+$/",$name)){   //判断是否为纯字母
            echo  1308;
            die;
        }
        /* __EXT__ 获得后缀名 */
        if(__EXT__==""){ //判断对方需要数据格式
            $type="html";
        }else{
            $type=__EXT__;
        }
        switch ($this->_method){ //判断请求方式
            case ‘get‘:
                    $data=$this->sel($name);
                    $this->response($data,$type); //转换格式并输出
                break;
            default:break;
        }
    }

    public function sel($dizhi){
        $sheng=M("sheng");
        $city=M("city");
        $xian=M("xian");
        if($dizhi==‘china‘){
            $res=$sheng->select();
            return $res;
        }else{
            $sid=$sheng->getFieldByPyname($dizhi,‘id‘); //相当于 select id from sheng where pyname = $dizhi
            if($sid==""){
                $cid=$city->getFieldByPyname($dizhi,‘id‘);
                if($cid==""){
                    $xid=$xian->getFieldByPyname($dizhi,‘id‘);
                        if($xid==""){
                            echo "县级数据请查上一级";
                        }else{
                            $condition[‘id‘] = $xid;
                            $data=$xian->where($condition)->select();
                            return $data;
                        }
                    }else{
                         $condition[‘p_id‘] = $cid;
                        $data=$xian->where($condition)->select();
                         return $data;
                    }
                }else{
                    $condition[‘p_id‘] = $sid;
                    $data=$city->where($condition)->select();
                    return $data;
            }
        }
    }
}


ThinkPHP 中国天气接口事例

标签:thinkphp   接口   天气   

原文地址:http://blog.csdn.net/demon0313/article/details/45741351

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