小编给大家分享一下ThinkPHP3.2天气接口怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
首先在配置文件中配置路由
'URL_ROUTER_ON' => true,
'URL_ROUTE_RULES'=>array(
'index/:wather' => 'Api/read',
),
在控制器中
<?php
// 本类由系统自动生成,仅供测试用途
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天气接口怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!