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; } } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。