小編給大家分享一下ThinkPHP3.2天氣接口怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)專注于中衛(wèi)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供中衛(wèi)營(yíng)銷型網(wǎng)站建設(shè),中衛(wèi)網(wǎng)站制作、中衛(wèi)網(wǎng)頁設(shè)計(jì)、中衛(wèi)網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造中衛(wèi)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供中衛(wèi)網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
首先在配置文件中配置路由
'URL_ROUTER_ON' => true,
'URL_ROUTE_RULES'=>array(
'index/:wather' => 'Api/read',
),
在控制器中
// 本類由系統(tǒng)自動(dòng)生成,僅供測(cè)試用途
namespace Home\Controller; //表示當(dāng)前類是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 "格式錯(cuò)誤";
die;
}
if(__EXT__ == ""){
$type="html";
}else{
$type=__EXT__;
}
//var_dump($type);die;
switch ($this->_method){
case 'get': // get請(qǐng)求處理代碼
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請(qǐng)求處理代碼
break;
case 'post': // post請(qǐng)求處理代碼
break;
}
}
還有一種方法
配置文件不變
寫一個(gè)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;
在控制器中
// 本類由系統(tǒng)自動(dòng)生成,僅供測(cè)試用途
namespace Home\Controller; //表示當(dāng)前類是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 "格式錯(cuò)誤";
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 "格式錯(cuò)誤";
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 "格式錯(cuò)誤";
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'); //根據(jù)用戶的name獲取用戶的id值
//print_r($sid);die;
if($sid == ""){
$cid=$city->getFieldByPyname($dizhi,'id');
if($cid == ""){
$xid=$xian->getFieldByPyname($dizhi,'id');
if($xid ==""){
echo "沒有此地區(qū)的天氣情況";
}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天氣接口怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!