First configure the route in the configuration file
' url_router_on ' = true,
' Url_route_rules ' =>array (
' Index/:wather ' = ' api/read ',
),
In the controller
<?php
This class is automatically generated by the system and is intended for testing purposes only
namespace Home\controller; Indicates that the current class is the Controller class under the Home module
Use Think\controller\restcontroller; Represents the introduction of the Think\controller namespace for easy direct use
Class Apicontroller extends restcontroller{
/*
Public Function index () {
$name = $_get[' wather ');
if (!preg_match ("/^[a-za-z\s]+$/", $name)) {
echo "Malformed";
Die
}
if (__ext__ = = "") {
$type = "html";
}else{
$type =__ext__;
}
Var_dump ($type);d ie;
Switch ($this->_method) {
Case ' get '://GET request processing code
if (__ext__ = = ' html ') {
Echo 1;
$data = $this->seach ($name);
Print_r ($data);d ie;
$return = $this->response ($data, ' html ');
Print_r ($return);d ie;
return $return;
}elseif (__ext__ = = ' xml ') {
Echo 2;
$data = $this->seach ($name);
Print_r ($data);d ie;
$return = $this->response ($data, ' xml ');
Print_r ($return);d ie;
return $return;
}elseif (__ext__ = = ' json ') {
Echo 3;
$data = $this->seach ($name);
Print_r ($data);d ie;
$return = $this->response ($data, ' json ');
Print_r ($return);d ie;
return $return;
}
Break
Case ' put '://Put request processing code
Break
Case ' post '://POST request processing code
Break
}
}
There's another way.
The configuration file does not change
Write a PHP page
<?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;
In the controller
<?php
This class is automatically generated by the system and is intended for testing purposes only
namespace Home\controller; Indicates that the current class is the Controller class under the Home module
Use Think\controller\restcontroller; Represents the introduction of the Think\controller namespace for easy direct use
Class Apicontroller extends restcontroller{
/**/
Public Function read_get_html () {
Echo 1;
$name = $_get[' wather ');
if (!preg_match ("/^[a-za-z\s]+$/", $name)) {
echo "Malformed";
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 "Malformed";
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 "Malformed";
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);d ie;
return $res;
}else{
$sid = $sheng->getfieldbypyname ($dizhi, ' id '); Gets the user's ID value based on the user's name
Print_r ($SID);d ie;
if ($sid = = "") {
$cid = $city->getfieldbypyname ($dizhi, ' id ');
if ($cid = = "") {
$xid = $xian->getfieldbypyname ($dizhi, ' id ');
if ($xid = = "") {
echo "No weather conditions in this area";
}else{
$condition [' p_id '] = $XID;
$res = $xian->where ($condition)->select ();
Print_r ($res);
return $res;
}
}else{
$condition [' p_id '] = $cid;
Print_r ($condition);d ie;
$res = $xian->where ($condition)->select ();
Print_r ($res);
return $res;
}
}else{
$condition [' p_id '] = $sid;
Print_r ($condition [' p_id ']);d ie;
$res = $city->where ($condition)->select ();
Print_r ($res);d ie;
return $res;
}
}
}
}
ThinkPHP3.2 Weather Interface