ThinkPHP实现天气预报接口

API 说明

  1. http://IP/weather/100100001 根据 citycode 获取城市天气信息(JSON 格式)
    返回:{“code”:200,“weather_info”:“sunny”}
    code 200表示请求正确,code 404表示请求错误
 
namespace app\api\model;

use think\Model;
use think\Db;

// http://IP/weather/100100001  根据 citycode 获取城市天气信息(JSON 格式)

class Weather extends Model
{
    public function getWeather($citycode = '101010100') // default code Beijing
    {
        $res = Db::name('ins_county')->where('weather_code', $citycode)->value('weather_info');
        return $res;
    }
}
 
namespace app\api\model;

use think\Model;
use think\Db;

// http://IP/city/北京   根据城市名称获取 citycode(JSON格式)

class Citycode extends Model
{
    public function getCode($county_name = '北京')
    {
        // $res = Db::name('ins_county')->where('county_name', $county_name)->select();
      	$res = Db::name('ins_county')->where('county_name', $county_name)->value('weather_code');
        return $res;
    }

}


namespace app\api\controller;

use think\Controller;

// http://IP/city/北京   根据城市名称获取 citycode(JSON格式
class Citycode extends Controller{
	public function read(){
      $cityName = input('cityName');
      $model = model('Citycode');
      $data = $model->getCode($cityName);
      if($data){
        $code = 200;
      }else{
      	$code = 404;
      }
      $data = [
      	'code' => $code,
        'citycode' => $data
      ];
      return json($data);
    }
}

你可能感兴趣的:(Web)