首页 > 代码库 > 微信订阅号(第五个版本)总结
微信订阅号(第五个版本)总结
<?php /** * wechat php test */ //define your token define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); //$wechatObj->valid(); $wechatObj->responseMsg(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()) { echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)) { $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; //$type=$postobj->MsgType; $type =$postObj->MsgType; $customevent=$postObj->Event; $latitude=$postObj->Location_X; $longitude=$postObj->Location_Y; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; $musicTpl="<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music> </xml>"; $newsTpl="<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>2</ArticleCount> <Articles> <item> <Title><![CDATA[女神的气质大衣]]></Title> <Description><![CDATA[一款舒适的长大衣是寒冷季节的首选]]></Description> <PicUrl><![CDATA[http://4.zhangxuman.sinaapp.com/1.jpg]]></PicUrl> <Url><![CDATA[http://www.baidu.com]]></Url> </item> <item> <Title><![CDATA[墨概念呢子外套]]></Title> <Description><![CDATA[秋冬女装大衣2014新款韩版修身大毛领毛呢外套]]></Description> <PicUrl><![CDATA[http://4.zhangxuman.sinaapp.com/2.jpg]]></PicUrl> <Url><![CDATA[http://www.baidu.com]]></Url> </item> </Articles> </xml>"; if($type=="event" )//and $customrevent=="subscribe") { $msgType = "text"; $contentStr="感谢您的关注\n 回复1可查看联系方式 \n 回复2可还钱 \n 回复3可查看各种小吃 \n 回复‘查询天气#城市名称‘可查询天气"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } else { switch($type) { case "text": { if(!empty( $keyword )) { if($keyword=="1") { $msgType = "text"; $contentStr="广东工业大学工二馆,联系方式:18790652159,联系人:张星星"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } else if($keyword=="2") { $msgType = "text"; $contentStr="请将钱打入工行卡88888888,于五个工作日内,谢谢配合"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } else if($keyword=="3") { $msgType = "text"; $contentStr="饿了吗?河南小吃,广东小吃,台湾小吃,各种美味等着你哦"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } else if($keyword=="菜单") { $msgType = "text"; $contentStr="感谢您的关注\n 回复1可查看联系方式 \n 回复2可还钱 \n 回复3可查看各种小吃 \n 回复‘查询天气#城市名称‘可查询天气"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } else if($keyword=="音乐") { $Title="纯真年代"; $Description="离婚前规则主题曲"; $MusicUrl="http://1.zhangxuman.sinaapp.com/music/1.mp3"; $HQMusicUrl= $MusicUrl; $msgType="music"; $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$Title,$Description,$MusicUrl,$HQMusicUrl); } else if($keyword=="查询天气#weatherurl") { $msgType = "text"; $weatherurl="http://api.map.baidu.com/telematics/v2/weather?location={$keyword}&ak=8UGHSPObt5WTKQrotMFlXuSI"; //天气预报接口 $apistr=file_get_contents($weatherurl); $apiobj=simplexml_load_string($apistr); $placeobj=$apiobj->currentCity; $todayobj=$apiobj->results->result[0]->date; $weatherobj=$apiobj->results->result[0]->weather; $windobj=$apiobj->results->result[0]->wind; $temobj=$apiobj->results->result[0]->temperature; $contentStr="{$placeobj}{$todayobj}天气{$weatherobj},风力{$windobj},温度{$temobj}"; $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } else if($keyword=="应用") { $msgType = "text"; $contentStr="\n<a href=http://www.mamicode.com/"http://haotianqi.duapp.com" >天气查询/n/n/n"; $resultStr = sprintf($textTpl2, $fromUsername, $toUsername, $time, $msgType, $contentStr); } else if($keyword=="购物时尚") { //$msgType="text"; $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time); } else //这个else里面是调用图灵机的apidai‘m { $apiKey = "70fc220d8d6364d9a6fe894d7ec7ec59"; $apiURL = "http://www.tuling123.com/openapi/api?key=KEY&info=INFO";//2 3 1 $url = str_replace("INFO", $keyword, str_replace("KEY", $apiKey, $apiURL)); /** 方法一、用file_get_contents 以get方式获取内容 */ $res =file_get_contents($url); //这三句是解析api返回的res,并用contentStr输出 $apiObj =json_decode($res); $contentStr = $apiObj->text; $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } } else { $contentStr= "Input something..."; } break; } case "voice": { $msgType = "text"; $contentStr="语音功能尚未完善,请输入文本信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } case "image": { $msgType = "text"; $contentStr="你的图片很漂亮"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } case "location": { $msgType = "text"; $contentStr="你的纬度是{$latitude},经度是{$longitude},我们已经锁定!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } case "link": { $msgType = "text"; $contentStr="你的链接有病毒吧!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } default : { $msgType = "text"; $contentStr="此功能尚未开发"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } } } echo $resultStr; } else { exit; } } private function checkSignature() { $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 ) { return true; }else { return false; } } } ?>
微信订阅号(第五个版本)总结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。