您的位置:首页 - 教程 - 微信 - 正文
微信开发模式,简单能运行的例子

<?php /** * wechat php test */ //define your token define("TOKEN", "weixin"); //设置token常量 $wechatObj = new wechatCallbackapiTest(); //实例化wechatCallbackapiTest对象 if (!isset($_GET['echostr'])) { $wechatObj->responseMsg(); //执行wechatCallbackapiTest类下的responseMsg()方法 }else{ $wechatObj->valid(); //执行wechatCallbackapiTest类下的valid()方法 } class wechatCallbackapiTest //创建wechatCallbackapiTest类 { public function valid() //创建一个名为valid()的公共函数方法 { $echoStr = $_GET["echostr"]; //收集get数据(随机字符串) //valid signature , option if($this->checkSignature()){ //判断checkSignature()自定义函数结果(验证微信服务器) echo $echoStr; //为真则输出结果(随机字符串) exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//获取post数据 //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $type = $postObj->MsgType; $customrevent = $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>"; switch ($type) { case "event"; if ($customrevent=="subscribe") {$contentStr = "亲,你终于来啦~~感谢关注号!";} break; case "image"; $contentStr = "你的图片很棒!"; break; case "voice": $contentStr = "我不知道你在说什么!还是发送文字吧!\n"; break; case "video"; $contentStr = "你的视频很棒!"; break; case "location"; $data = file_get_contents("http://api.map.baidu.com/geoconv/v1/?coords={$latitude},{$longitude}&output=xml&from=3&to=5&ak=你的ak"); $array = simplexml_load_string($data); $lat=$array->result->point[0]->x; $log=$array->result->point[0]->y; // 腾讯地图坐标转换为百度地图坐标 $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>4</ArticleCount> <Articles> <item> <Title><![CDATA[百度地图导航]]></Title> <Description><![CDATA[]]></Description> <PicUrl><![CDATA[]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> <item> <Title><![CDATA[【公交线路导航】]]></Title> <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://pic.58pic.com/58pic/12/86/57/42C58PICikN.jpg]]></PicUrl> <Url><![CDATA[http://api.map.baidu.com/direction?origin={$lat},{$log}&destination=latlng:32.525187,114.375606|name:公司&mode=transit&region=武汉&output=html]]></Url> </item> <item> <Title><![CDATA[【驾车线路导航】]]></Title> <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://pic.58pic.com/58pic/12/18/91/80i58PIC7M2.jpg]]></PicUrl> <Url><![CDATA[http://api.map.baidu.com/direction?origin={$lat},{$log}&destination=latlng:32.525187,114.375606|name:公司&mode=driving&region=武汉&output=html]]></Url> </item> <item> <Title><![CDATA[【步行线路导航】]]></Title> <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://pic.58pic.com/58pic/12/86/62/54r58PICWci.jpg]]></PicUrl> <Url><![CDATA[http://api.map.baidu.com/direction?origin={$lat},{$log}&destination=latlng:32.525187,114.375606|name:公司&mode=walking&region=武汉&output=html]]></Url> </item> </Articles> <FuncFlag>0</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $url); echo $resultStr; break; case "link" ; $contentStr = "你的链接有病毒吧!"; break; default; $contentStr ="此项功能尚未开发"; } if($keyword=="天气"){ // 天气api接口 http://developer.baidu.com/map/carapi-7.htm $weatherurl="http://api.map.baidu.com/telematics/v2/weather?location=武汉&ak=你的ak"; $apistr=file_get_contents($weatherurl); $apiobj=simplexml_load_string($apistr); $contentStr ="【武汉天气预报】\n{$apiobj->results->result[0]->date}\n天气 {$apiobj->results->result[0]->weather}\n风力 {$apiobj->results->result[0]->wind}\n温度 {$apiobj->results->result[0]->temperature} \n明天{$apiobj->results->result[1]->date}\n{$apiobj->results->result[1]->weather} {$apiobj->results->result[1]->wind} {$apiobj->results->result[1]->temperature} \n后天{$apiobj->results->result[2]->date}\n{$apiobj->results->result[2]->weather} {$apiobj->results->result[2]->wind} {$apiobj->results->result[2]->temperature} \n{$apiobj->results->result[3]->date}\n{$apiobj->results->result[3]->weather} {$apiobj->results->result[3]->wind} {$apiobj->results->result[3]->temperature}"; } if($keyword=="联系方式"){ $msgType="text"; $contentStr = "地址:"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } if($keyword=="图文消息"){ $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>3</ArticleCount> <Articles> <item> <Title><![CDATA[公司]]></Title> <Description><![CDATA[。。。]]></Description> <PicUrl><![CDATA[http://mmbiz.qic.cn/mmbiz/pa10VgUEAzKVjPhPCLIR1s8dEFoApgY9teLw20KicoRicbn6MDy2MrxI5qJ587dHbZJ12dibYRP4KnbRJeLIcwA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1]]></PicUrl> <Url><![CDATA[http://mp.weiin.qq.com/s?__biz=MzI5OTE3MzkxMQ==mid=40267803&idx=1&sn=44c92d59ad3549cd3a5b37c75e2981b#rd]]></Url> </item> <item> <Title><![CDATA[。。]]></Title> <Description><![CDATA[。。]]></Description> <PicUrl><![CDATA[http://mmbiz.qpic.cn/mmiz/pa10VgUEAzKVjEhPCLIR1s8dEFoApgYyYqlLK8axnexnJtreBHjgiaKGm5Zb5UsZoZbkBOdjibjv5bSicoQ0e1gQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1]]></PicUrl> <Url><![CDATA[http://mp.weixin.qq.com/s?__biz=MzI5OTE3MzkxMQ==&mid402678184&idx=1&sn=3ea48613eb1011e1e773a339f58e793e#rd]]></Ur </item> <item> <Title><![CDATA[test]]></Title> <Description><![CDATA[test]]></Description> <PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz/pa10VUEAzJrKQTKqehB5tBDr6fGJwicictMgVr7RPdNmib0sCkt8SckGV637jOa3xMPnSuCCRLfO2VhDsjLpgWQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1]]></PicUrl> <Url><![CDATA[http://mp.weixin.qq.co/s?__biz=MzI5OTE3MzkxMQ==mid=40260666&idx=1&sn=220074c46880b11b3c038ce1e405596c#rd]]></Url> </item> </Articles> <FuncFlag>0</FuncFlag> </xml>";
          // 以上标红的地方都需要自己修改
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$url); echo $resultStr; } if(keyword=="历史消息"){ $url = "历史消息网址"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$url); echo $resultStr; } $msgType="text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else { echo ""; 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; } } } ?>

  注意修改相关内容,即可成功运行。

  有点杂乱,不喜勿喷。


评论: