欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

微信公众平台开发尝试,微信公众平台_PHP教程

程序员文章站 2022-06-06 14:51:45
...

微信公众平台开发尝试,微信公众平台

最近看了一篇博文是说微信公众平台搭建,于是心血来潮自己也尝试了一下。

1.不用多讲了,申请微信公众号,对于个人来说,只能申请订阅号。

可以看这个链接的文章,http://jingyan.baidu.com/article/414eccf6011ff06b431f0aec.html

2.采用的是Sina App Engine来脱管服务,就要申请sae开发者权限。

再看这个链接http://jingyan.baidu.com/article/4f7d57129a60531a20192796.html

3.在sae上新建一个应用

微信公众平台开发尝试,微信公众平台_PHP教程

微信公众平台开发尝试,微信公众平台_PHP教程

我选择的PHP,PHP几乎免费。Java应用的话,有点小贵了。

虽然没有学过PHP,摸着石头过河吧。

将php代码上传后,就有一个可调试的url了,PHP代码后面上。

微信公众平台开发尝试,微信公众平台_PHP教程

4.再在微信公众平台中的开发者中心(这个要经过一段时间的审核),填入一下内容即可

微信公众平台开发尝试,微信公众平台_PHP教程

5.然后就是对接数据了,为了测试,我对接的是图灵机器人的数据、12306和百度地图API

12306是为了查余票和车次,百度地图API是为了查天气预报、图灵机器人见链接:http://www.tuling123.com/openapi/

php代码如下,本人不是搞PHP的,因此只能在网上拷贝了,如有复制,纯属巧合。

微信公众平台开发尝试,微信公众平台_PHP教程微信公众平台开发尝试,微信公众平台_PHP教程php define("TOKEN", "weixin"); Session_start(); Session_Register("kw"); $wechatObj = new wechatCallbackapiTest(); require_once("train.php"); $tqTpl = "%s5 "; 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); //消息类型分离 switch ($RX_TYPE) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveTulingText($postObj); break; case "image": $result = $this->receiveImage($postObj); break; case "location": $result = $this->receiveLocation($postObj); break; case "voice": $result = $this->receiveVoice($postObj); break; case "video": $result = $this->receiveVideo($postObj); break; case "link": $result = $this->receiveLink($postObj); break; default: $result = "unknown msg type: ".$RX_TYPE; break; } $this->logger("T ".$result); echo $result; }else { echo ""; exit; } } //接收事件消息 private function receiveEvent($object) { $content = ""; switch ($object->Event) { case "subscribe": $content = "欢迎关注TomSnail "; $content .= (!empty($object->EventKey))?("\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):""; break; case "unsubscribe": $content = "取消关注"; break; case "SCAN": $content = "扫描场景 ".$object->EventKey; break; case "CLICK": switch ($object->EventKey) { case "COMPANY": $content = array(); $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); break; default: $content = "点击菜单:".$object->EventKey; break; } break; case "LOCATION": $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude; break; case "VIEW": $content = "跳转链接 ".$object->EventKey; break; case "MASSSENDJOBFINISH": $content = "消息ID:".$object->MsgID.",结果:".$object->Status.",粉丝数:".$object->TotalCount.",过滤:".$object->FilterCount.",发送成功:".$object->SentCount.",发送失败:".$object->ErrorCount; break; default: $content = "receive a new event: ".$object->Event; break; } if(is_array($content)){ if (isset($content[0])){ $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 receiveTulingText($object){ try{ $keyword = trim($object->Content); $userid = trim($object->FromUserName); $apiKey = "*******************************"; $apiURL = "http://www.tuling123.com/openapi/api?key=".$apiKey."&info=".$keyword."&userid=".userid; if((substr_count($keyword,'天气')!=0)||$_SESSION["kw"]=="天气"){ //百度地图API if($keyword=="天气"){ $_SESSION["kw"]="天气"; $content = "请输入城市".$_SESSION["kw"]; }else{ $geshu = substr_count($keyword,'天气'); if($geshu==0){ $city = $keyword; }else{ $t = explode("天气",$keyword); for($i=0;$i$geshu