微信天气预报二

城市名称和对应的编号已经入库,接下就是应用到微信当中了,根据用户输入的城市名称查询中对应的天气情况

我将城市天气查询单独写在一个php文件中,代码如下:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include_once('ini.php');//ini.php是我的mysql连接的一个类
class tianqi extends inc {
private $names;
private $inc;

public function __construct($names){
$this->inc = new inc();
$this->names = $names;
set_time_limit(0);
}
public function get_tianqi() {
$id = $this->get_id($this->names);
if($id){
$url = 'http://m.weather.com.cn/data/'.$id.'.html';

$curl = curl_init(); //开启curl
curl_setopt($curl, CURLOPT_URL, $url); //设置请求地址
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  //是否输出 1 or true 是不输出 0  or false输出


$data = curl_exec($curl); //执行curl操作
curl_close($curl);
$data = json_decode($data);
return "城市:".$data->weatherinfo->city." 时间:".$data->weatherinfo->date_y." ".$data->weatherinfo->week." 气温:".$data->weatherinfo->temp1." 天气状况:".$data->weatherinfo->wind1.$data->weatherinfo->fl1.",".$data->weatherinfo->index_d;
} else {
return "抱歉!没有查到您需要的城市!";
}
}
private function get_id($city_name){
$sql = "SELECT city_bh FROM city_id WHERE city_name = '{$city_name}'";
$rel = mysql_query($sql);
$row = mysql_fetch_array($rel);
$id = $row['city_bh'];
return $id;
}
}
?>

注释很清楚了不需要解释了,微信入口文件如下,同样采用的是php官方的demo,纯文本消息,具体代码省略

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

$keyword = trim($postObj->Content);
// 其他地方都是一样,所以就不贴代码了,只是在这里获取的天气查询的结果
$robot = new robot($keyword);
$cont = $robot->get_content();

                $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";             
if(!empty( $keyword ))
                {
              $msgType = "text";
                $contentStr = $cont;//将天气情况赋值给回复的消息内容
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
                }

预览效果

获取天机的接口地址

'http://m.weather.com.cn/data/'.$id.'.html';

"weatherinfo": {
        "city": "哈尔滨", // 城市中文名
        "city_en": "haerbin", // 城市英文名
        "date_y": "2012年8月18日", // 发布日期
        "date": "", // ?
        "week": "星期六", // 周信息
        "fchh": "18", // 信息发布时的整点小时数(感谢读者 白白乌鸦 的提醒)
        "cityid": "101050101", // 城市ID
        "temp1": "18℃~26℃", // 今日气温
        "temp2": "17℃~29℃", // 明日气温
        "temp3": "18℃~23℃", // 第三日气温
        "temp4": "13℃~24℃", // 第四日气温
        "temp5": "15℃~31℃", // 第五日气温
        "temp6": "14℃~32℃", // 第六日气温
        "tempF1": "64.4℉~78.8℉", // 今日气温(华氏)
        "tempF2": "62.6℉~84.2℉", // 明日气温(华氏)
        "tempF3": "64.4℉~73.4℉", // 第三日气温(华氏)
        "tempF4": "55.4℉~75.2℉", // 第四日气温(华氏)
        "tempF5": "59℉~87.8℉", // 第五日气温(华氏)
        "tempF6": "57.2℉~89.6℉", // 第六日气温(华氏)
        "weather1": "多云", // 今日天气
        "weather2": "晴转多云", // 明日天气
        "weather3": "雷阵雨转小雨", // 第三日天气
        "weather4": "多云", // 第四日天气
        "weather5": "晴", // 第五日天气
        "weather6": "晴", // 第六日天气
        "img1": "1", // 天气图标编号,此处的编号及其图片获取规则尚不清楚,如有知道详情的恳请评论告知,我将添加说明
        "img2": "99", // 天气图标编号
        "img3": "0", // 天气图标编号
        "img4": "1", // 天气图标编号
        "img5": "4", // 天气图标编号
        "img6": "7", // 天气图标编号
        "img7": "1", // 天气图标编号
        "img8": "99", // 天气图标编号
        "img9": "0", // 天气图标编号
        "img10": "99", // 天气图标编号
        "img11": "0", // 天气图标编号
        "img12": "99", // 天气图标编号
        "img_single": "1", // ? 可能是天气图标编号
        "img_title1": "多云", // ? 可能是天气图标对应的 title
        "img_title2": "多云", // ? 可能是天气图标对应的 title
        "img_title3": "晴", // ? 可能是天气图标对应的 title
        "img_title4": "多云", // ? 可能是天气图标对应的 title
        "img_title5": "雷阵雨", // ? 可能是天气图标对应的 title
        "img_title6": "小雨", // ? 可能是天气图标对应的 title
        "img_title7": "多云", // ? 可能是天气图标对应的 title
        "img_title8": "多云", // ? 可能是天气图标对应的 title
        "img_title9": "晴", // ? 可能是天气图标对应的 title
        "img_title10": "晴", // ? 可能是天气图标对应的 title
        "img_title11": "晴", // ? 可能是天气图标对应的 title
        "img_title12": "晴", // ? 可能是天气图标对应的 title
        "img_title_single": "多云", // ? 可能是天气图标对应的 title
        "wind1": "西南风小于3级转西风3-4级", // 今日风向风力信息
        "wind2": "西风小于3级转西南风3-4级", // 明日风向风力信息
        "wind3": "西南风小于3级转3-4级", // 第三日风向风力信息
        "wind4": "西南风小于3级转3-4级", // 第四日风向风力信息
        "wind5": "西南风小于3级转3-4级", // 第五日风向风力信息
        "wind6": "西南风小于3级转3-4级", // 第六日风向风力信息
        "fx1": "西南风", // ? 
        "fx2": "西风", // ? 
        "fl1": "小于3级转3-4级", // 今日风力信息
        "fl2": "小于3级转3-4级", // 明日风力信息
        "fl3": "小于3级转3-4级", // 第三日风力信息
        "fl4": "小于3级转3-4级", // 第四日风力信息
        "fl5": "小于3级转3-4级", // 第五日风力信息
        "fl6": "小于3级转3-4级", // 第六日风力信息
        "index": "热",
        "index_d": "天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单裤。",
        "index48": "炎热",
        "index48_d": "天气炎热,建议着短衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫等清凉夏季服装。",
        "index_uv": "中等", // 紫外线信息
        "index48_uv": "弱", // 48 小时紫外线信息
        "index_xc": "较适宜", // 洗车指数(感谢读者 刘睿 的提醒)
        "index_tr": "适宜", // 旅游指数
        "index_co": "舒适", // 舒适指数
        "st1": "25",
        "st2": "17",
        "st3": "28",
        "st4": "19",
        "st5": "18",
        "st6": "16",
        "index_cl": "较适宜", // 晨练指数
        "index_ls": "适宜", // 晾晒指数
        "index_ag": "极易发" //


关注微信账号z510168343


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值