您的位置:首页 > 移动开发 > 微信开发

微信开发(从申请微信到注册上线的一整套流程)第四章 实现天气预报功能

2016-10-05 14:14 1076 查看
 

这一章里,我们来实现微信上的天气预报功能,我们使用方倍工作室的天气预报接口,其接口为
http://apix.sinaapp.com/weather/
这个接口的参数appkey为公众号原始id,参数city为城市名

例如,查询深圳的天气预报时,将city值做urlencode,最终访问的url为
http://apix.sinaapp.com/weather/?appkey=trialuser&city=%E6%B7%B1%E5%9C%B3
返回的内容如下

[
{
"Title": "深圳天气预报",
"Description": "",
"PicUrl": "",
"Url": ""
},
{
"Title": "【实况】温度18℃ 湿度59%% 东北风2级 发布时间:08:55",
"Description": "",
"PicUrl": "",
"Url": ""
},
{
"Title": "【舒适】建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
"Description": "",
"PicUrl": "",
"Url": ""
},
{
"Title": "11月19日 周三 晴 23℃~17℃ 无持续风向 微风 日出日落:06:38~17:39",
"Description": "",
"PicUrl": "http://discuz.comli.com/weixin/weather/icon/d00.jpg",
"Url": ""
},
{
"Title": "11月20日 周四 多云 25℃~17℃ 无持续风向 微风 日出日落:06:39~17:38",
"Description": "",
"PicUrl": "http://discuz.comli.com/weixin/weather/icon/d01.jpg",
"Url": ""
},
{
"Title": "11月21日 周五 多云 26℃~18℃ 无持续风向 微风 日出日落:06:40~17:38",
"Description": "",
"PicUrl": "http://discuz.comli.com/weixin/weather/icon/d01.jpg",
"Url": ""
}
]

我们在微信中调用这一接口来获取天气预报信息,实现代码如下

<?php
/*
洋洋的博客
CopyRight 2014 All Rights Reserved
*/

define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}

class wechatCallbackapiTest
{
//验证签名
public function valid()
{
$echoStr = $_GET["echostr"];
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
echo $echoStr;
exit;
}
}

public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$this->logger("R ".$postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);

$result = "";
switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
case "text":
$result = $this->receiveText($postObj);
break;
}
$this->logger("T ".$result);
echo $result;
}else {
echo "";
exit;
}
}

private function receiveEvent($object)
{
switch ($object->Event)
{
case "subscribe":
$content = "欢迎关注洋洋的博客 ";
break;
}
$result = $this->transmitText($object, $content);
return $result;
}

private function receiveText($object)
{
$keyword = trim($object->Content);
$url = "http://apix.sinaapp.com/weather/?appkey=".$object->ToUserName."&city=".urlencode($keyword);
$output = file_get_contents($url);
$content = json_decode($output, true);

$result = $this->transmitNews($object, $content);
return $result;
}

private function transmitText($object, $content)
{
if (!isset($content) || empty($content)){
return "";
}
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}

private function transmitNews($object, $newsArray)
{
if(!is_array($newsArray)){
return "";
}
$itemTpl = " <item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($newsArray as $item){
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
}
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";

$result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
return $result;
}

private function logger($log_content)
{

}
}
?>
上述代码的下载地址为 http://pan.baidu.com/s/1gdsyHWJ,同样的方法,可将代码上传到SAE上。

在公众账号中使用的命令如下:

1. 发送城市名称,如“深圳”,可以查询该城市的天气

在你的公众账号输入相应的命令,实现效果类似如下所示:



总结:

总的来说,通过本教程,你得到了以下收获:

· 1. 你通过本教程得到了一个免费的新浪云计算空间,云计算哦

· 2. 你成功启用了开发模式,并且实现了时间的自动回复

· 3. 你了解了微信公众平台开发的原理,并且熟悉了各种消息及发送是怎么一回事

· 4. 你使用天气预报接口,实现了一个微信公众平台上的天气预报功能。

这个微信公众号暂时就说这么多!有什么疑问的可以留言哦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: