网站建设资讯

NEWS

网站建设资讯

ThinkPHP3.2天气接口怎么用

小编给大家分享一下ThinkPHP3.2天气接口怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

成都创新互联专注于中卫网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供中卫营销型网站建设,中卫网站制作、中卫网页设计、中卫网站官网定制、小程序定制开发服务,打造中卫网络公司原创品牌,更为您提供中卫网站排名全网营销落地服务。

首先在配置文件中配置路由

    'URL_ROUTER_ON'   => true,
    'URL_ROUTE_RULES'=>array(
        'index/:wather' => 'Api/read',
    ),

在控制器中

// 本类由系统自动生成,仅供测试用途
namespace Home\Controller;     //表示当前类是Home模块下的控制器类
use Think\Controller\RestController;          //表示引入 Think\Controller 命名空间便于直接使用
class ApiController extends RestController{
    /*
    public function index(){
    $name = $_GET['wather'];
    if(!preg_match("/^[a-zA-Z\s]+$/",$name)){
        echo  "格式错误";
        die;
    }
    if(__EXT__ == ""){
        $type="html";
    }else{
        $type=__EXT__;
    }
    //var_dump($type);die;
    switch ($this->_method){
        case 'get': // get请求处理代码
            if (__EXT__ == 'html'){
                //echo 1;
                $data=$this->seach($name);
                //print_r($data);die;
                $return = $this->response($data,'html');
                //print_r($return);die;
                //return $return;
            }elseif(__EXT__ == 'xml'){
                //echo 2;
                $data=$this->seach($name);
                //print_r($data);die;
                $return = $this->response($data,'xml');
                //print_r($return);die;
                //return $return;
            }elseif(__EXT__ == 'json'){
                //echo 3;
                $data=$this->seach($name);
                //print_r($data);die;
                $return = $this->response($data,'json');
                //print_r($return);die;
                //return $return;
            }
            break;
        case 'put': // put请求处理代码
            break;
        case 'post': // post请求处理代码
            break;
    }
}

还有一种方法

配置文件不变

写一个php页面

header("content-type:text/html;charset=utf-8");
include "./Snoopy.class.php";
$snoopy = new Snoopy();
//var_dump($snoopy);
$sourceURL = "http://localhost/ThinkPHP/index.php/Home/index/china.json";
$snoopy->rawheaders["accept"] = "application/json";
$snoopy->fetch($sourceURL);
echo $snoopy->results;

在控制器中

// 本类由系统自动生成,仅供测试用途
namespace Home\Controller;     //表示当前类是Home模块下的控制器类
use Think\Controller\RestController;          //表示引入 Think\Controller 命名空间便于直接使用
class ApiController extends RestController{

    /**/
    Public function read_get_html(){
        //echo 1;
        $name = $_GET['wather'];
        if(!preg_match("/^[a-zA-Z\s]+$/",$name)){
            echo  "格式错误";
            die;
        }
        $data=$this->seach($name);
        $return = $this->response($data,'html');
        return $return;
    }
    Public function read_get_xml(){
        //echo 2;
        $name = $_GET['wather'];
        if(!preg_match("/^[a-zA-Z\s]+$/",$name)){
            echo  "格式错误";
            die;
        }
        $data=$this->seach($name);
        $return = $this->response($data,'xml');
        return $return;
    }
    Public function read_get_json(){
        //echo 3;
        $name = $_GET['wather'];
        if(!preg_match("/^[a-zA-Z\s]+$/",$name)){
            echo  "格式错误";
            die;
        }
        $data=$this->seach($name);
        $return = $this->response($data,'json');
        return $return;
    }

    public function seach($dizhi){
        $sheng = M("sheng");
        $city = M("city");
        $xian = M("xian");
        if($dizhi == "china"){
            $res = $sheng->select();
            //print_r($res);die;
            return $res;
        }else{
            $sid=$sheng->getFieldByPyname($dizhi,'id');  //根据用户的name获取用户的id值
            //print_r($sid);die;
            if($sid == ""){
                $cid=$city->getFieldByPyname($dizhi,'id');
                if($cid == ""){
                    $xid=$xian->getFieldByPyname($dizhi,'id');
                    if($xid ==""){
                        echo "没有此地区的天气情况";
                    }else{
                        $condition['p_id'] = $xid;
                        $res=$xian->where($condition)->select();
                        //print_r($res);
                        return $res;
                    }
                }else{
                    $condition['p_id'] = $cid;
                    //print_r($condition);die;
                    $res=$xian->where($condition)->select();
                    //print_r($res);
                    return $res;
                }
            }else{
                $condition['p_id'] = $sid;
                //print_r($condition['p_id']);die;
                $res=$city->where($condition)->select();
                //print_r($res);die;
                return $res;
            }
        }
    }
}

以上是“ThinkPHP3.2天气接口怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


分享文章:ThinkPHP3.2天气接口怎么用
转载源于:http://njwzjz.com/article/pdpppe.html