收集天气预报城市代码并导入数据库;
微信天气预报开发
通过微信的输入找到对应的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("s elect appid f rom %t WHERE zimu=%s",
array($this->_table, $zimu));
if(!$appid){
$appid = DB::fetch_first("s elect appid f rom %t WHERE tpinyin=%s",
array($this->_table, $zimu));
}
if($appid){
return $appid;
}
}
public function fetch_appid_by_diqu($diqu) {
$appid = DB::fetch_first("s elect appid f rom %t WHERE diqu=%s",
array($this->_table, $diqu));
if($appid){
return $appid;
}
}
这个是通过城市代码获得城市天气预报情况
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'';
}
}
下面是效果图:
专业事找专业人做,一品威客聚集了五湖四海优质的服务商,如果你需要app开发,网络推广,设计,文案写作,营销策划等服务,欢迎到一品威客网发布相关任务,让广大的服务商为您解决难题。