20180813212252.png

使用PHP实现微信订阅号天气预报及论坛地址功能开发
index.php代码


<?php
/*
轩宇工作室
CopyRight 2014 All Rights Reserved
*/
//定义TOKEN
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); //判断天气 if (strstr($keyword, "天气")){ $city = str_replace('天气', '', $keyword);
include("weather.php"); $content = getWeatherInfo($city); //判断论坛,输入论坛直接发送图文消息并跳转到相应的链接地址 }else if (strstr($keyword, "论坛")){ $content[] = array("Title" =>"我们的论坛","Description" =>"", "PicUrl" =>"http://www.henkuai.com/template/mobanbus_qyv1/mobanbus_st/img//logo.png", "Url" =>"http://www.henkuai.com/"); }else{ $content = "功能未开发,请关注我们的最新消息。"."\n技术支持 轩宇工作室"."\n".date("Y-m-d H:i:s",time());
}

if(is_array($content)){ if (isset($content[0]['PicUrl'])){ $result = $this->transmitNews($object, $content); }else if (isset($content['MusicUrl'])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content);
}

return $result;
}

private function transmitText($object, $content)
{
if (!isset($content) || empty($content)){
return ""; } $textTpl = "

%s

"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result;
}

private function transmitNews($object, $newsArray)
{
if(!is_array($newsArray)){ return ""; } $itemTpl = "

<![CDATA[%s]]>


"; $item_str = ""; foreach ($newsArray as $item){ $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); } $newsTpl = "

%s %s $item_str

";

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

private function logger($log_content)
{

}
}
?>

weather.php代码
```php
<?php

// var_dump(getWeatherInfo("深圳"));

function getWeatherInfo($cityName)
{
if ($cityName == "" || (strstr($cityName, "+"))){
return "发送天气+城市,例如'天气深圳'";
}
$url = "http://api.map.baidu.com/telematics/v3/weather?location=".urlencode($cityName)."&output=json&ak=ECe3698802b9bf4457f0e01b544eb6aa";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$result = json_decode($output, true);
if ($result["error"] != 0){
return $result["status"];
}
$curHour = (int)date('H',time());
$weather = $result["results"][0];
$weatherArray[] = array("Title" =>$weather['currentCity']."天气预报", "Description" =>"", "PicUrl" =>"", "Url" =>"");
for ($i = 0; $i < count($weather["weather_data"]); $i++) {
$weatherArray[] = array("Title"=>
$weather["weather_data"][$i]["date"]."\n".
$weather["weather_data"][$i]["weather"]." ".
$weather["weather_data"][$i]["wind"]." ".
$weather["weather_data"][$i]["temperature"],
"Description"=>"", 
"PicUrl"=>(($curHour >= 6) && ($curHour < 18))?$weather["weather_data"][$i]["dayPictureUrl"]:$weather["weather_data"][$i]["nightPictureUrl"], "Url"=>"");
}
return $weatherArray;
}
?>
滇ICP备16003699号-2
最后修改:2018 年 08 月 13 日
如果觉得我的文章对你有用,请随意赞赏