微信天气预报开发

2025-09-26 05:54:02

1、收集天气预报城市代码并导入数据库;

微信天气预报开发

2、通过微信的输入找到对应的appid;通过getweather(appid)函数获取城市对应的天气;下面是找到对应appid的程序

if(preg_match("/^[A-Za-z]{2,}$/",$keyword)){ //如果输入的是字母;$appid = C::t('weixin_tianqi')->fetch_appid_by_zimu($keyword);

if($appid){$weather = getw($appid['appid'

}

return $weather;

}else{//如果输入的是汉子;

$appid = C::t('weixin_tianqi')->fetch_appid_by_diqu($keyword);

if($appid){

$weather = getw($appid['appid']);

return $weather;

}

}

下面这个是数据库的程序

public function fetch_appid_by_zimu($zimu) {

$appid = DB::fetch_first("SELECT appid FROM %t WHERE zimu=%s",

array($this->_table, $zimu));

if(!$appid){

$appid = DB::fetch_first("SELECT appid FROM %t WHERE tpinyin=%s",

array($this->_table, $zimu));

}

if($appid){

return $appid;

}

}

public function fetch_appid_by_diqu($diqu) {

$appid = DB::fetch_first("SELECT appid FROM %t WHERE diqu=%s",

array($this->_table, $diqu));

if($appid){

return $appid;

}

}

3、这个是通过城市代码获得城市天气预报情况

function getweather($appid){

$appid=trim($appid);

if ($appid){

$content = file_get_contents("http://m.weather.com.cn/data/{$appid}.html");

if(!$content){

//mysql_query("insert into errorlog values(null,'没有取到天气接口数据','$date',$time)",$link);

//echo mysql_error();

file_put_contents('weather.txt', '没有接受到天气数据', FILE_APPEND);

return '暂时没有取到天气数据,请稍后再试';

}

$result=json_decode($content,true);

$info=$result['weatherinfo'];

$strw="{$info['city']}的天气情况\n";

$strw .= "今天:({$info['temp1']}){$info['weather1']}{$info['wind1']}{$info['fl1']},";

$strw .= "明天:({$info['temp2']}){$info['weather2']}{$info['wind2']}{$info['fl2']},";

$strw .= "今天:{$info['date_y']}{$info['week']}。";

return $strw;

}else{

return '没有获取到该城市的天气,请确定输入了正确的城市名称,如\'北京\'或者\'beijing\'';

}

}

4、下面是效果图:

微信天气预报开发

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢