10大优享服务
62项会员特权

微信天气预报开发

发布时间:2021-12-01 14:04:26     阅读次数:8817次     评论数:0次

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

微信天气预报开发

  

  通过微信的输入找到对应的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开发,网络推广设计,文案写作,营销策划等服务,欢迎到一品威客网发布法律服务相关任务,让广大的服务商为您解决难题。

本文地址:
来源:一品威客,转载须经版权人书面授权并注明来源

留言(0

↓展开留言

该攻略尚无留言记录