首先在配置文件中配置路由
‘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 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页面
<?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;
在控制器中
<?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;
}
}
}
}
原文地址:http://10182603.blog.51cto.com/10172603/1651643