源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

微信公众平台开发入门教程(SAE方倍工作室)

  • 时间:2020-12-17 14:54 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:微信公众平台开发入门教程(SAE方倍工作室)
我们将使用微信公众账号方倍工作室作为讲解的例子,二维码见底部。 本系列教程将引导你完成如下任务: 创建新浪云计算平台应用启用微信公众平台开发模式基础接口消息及事件微信公众平台PHP SDK微信公众平台开发模式原理开发天气预报功能 [b]第一章 申请服务器资源[/b] 创建新浪云计算应用 申请账号 我们使用SAE新浪云计算平台作为服务器资源,并且申请PHP环境+MySQL数据库作为程序运行环境。 申请地址:[url=http://sae.sina.com.cn/]http://sae.sina.com.cn/[/url] ,使用新浪微博账号可以直接登录SAE,登录后SAE将赠送500个免费云豆。 [img]http://files.jb51.net/file_images/article/201405/201405191418102.jpg[/img] [b]创建新应用 [/b] 登录后点击顶部【[b]我的首页[/b]】 [img]http://files.jb51.net/file_images/article/201405/201405191418103.png[/img] 点击下侧的创建新应用,这时会弹出提示, 禁止放置违法违规内容,点击继续创建,弹出如下窗口。  [img]http://files.jb51.net/file_images/article/201405/201405191418104.jpg[/img] 选择一个未使用的appid,如果老是已经被使用不知道该什么好,就填写你的QQ号或者手机号吧。 填写二级域名AppID、应用名称、验证码,开发语言选择PHP,应用类型选择web应用。然后点击创建应用  [img]http://files.jb51.net/file_images/article/201405/201405191418105.jpg[/img] 应用创建成功。并自动跳转到应用列表中,可以看到已经有刚才创建的CCTV-7  [img]http://files.jb51.net/file_images/article/201405/201405191418106.jpg[/img]   [b]创建版本 [/b] 选择CCTV-7右侧的[b]应用管理[/b]下面的[b]代码管理[/b],  [img]http://files.jb51.net/file_images/article/201405/201405191418107.jpg[/img] 跳转到代码管理 [img]http://files.jb51.net/file_images/article/201405/201405191418108.jpg[/img] 点击右侧的[img]http://files.jb51.net/file_images/article/201405/201405191418109.jpg[/img] [img]http://files.jb51.net/file_images/article/201405/2014051914181010.jpg[/img] 版本号默认为1,点击创建,成功后如下图所示: [img]http://files.jb51.net/file_images/article/201405/2014051914181011.jpg[/img]   到这里,就成功创建了一个域名URL为 http://cctv7.sinaapp.com/ 的应用了。 [b]上传代码 [/b] 将以下代码复制下来,另存为index.php。必须使用专业的开发编辑软件操作,例如Notepad++,不要使用Windows自带的记事本等。
[url=http://xiazai.jb51.net/201405/yuanma/android/index.zip]点此下载[/url] 在代码管理界面中,选择操作按钮。 [img]http://files.jb51.net/file_images/article/201405/2014051914181013.jpg[/img] 选择上传代码包。 点击上传文件,选择刚才压缩好的index.zip文件,点击上传,上传成功后如下所示,[b]如果上传有问题,请在Chrome浏览器下重试一下。[/b] [img]http://files.jb51.net/file_images/article/201405/2014051914181014.jpg[/img] 点击操作按钮下的代码编辑,  [img]http://files.jb51.net/file_images/article/201405/2014051914181015.jpg[/img] 我们可以看到index.php已经上传成功,双击可以查看编辑里面的代码 [img]http://files.jb51.net/file_images/article/201405/2014051914181016.jpg[/img] 新浪云应用的创建就成功了。   [b]第二章 启用开发模式[/b] [b]微信公众平台开发模式[/b] [b]高级功能[/b] 微信公众平台地址:[url=https://mp.weixin.qq.com/]https://mp.weixin.qq.com[/url]  登录微信公众平台后台,选择高级功能,进入后就看到两种模式 [img]http://files.jb51.net/file_images/article/201405/2014051914181017.jpg[/img] 我们需要先关闭编辑模式。点击编辑模式的进入 [img]http://files.jb51.net/file_images/article/201405/2014051914181018.jpg[/img] 滑动关闭 [img]http://files.jb51.net/file_images/article/201405/2014051914181019.jpg[/img]   [b]开发模式[/b] 进入开发模式里面 [img]http://files.jb51.net/file_images/article/201405/2014051914181020.jpg[/img]  点击成为开发者 [img]http://files.jb51.net/file_images/article/201405/2014051914181021.jpg[/img] 弹出URL和Token填写框 [img]http://files.jb51.net/file_images/article/201405/2014051914181022.jpg[/img] 此处的URL为上篇中介绍的云应用的域名,而Token在index.php中定义为weixin。提交后提示你已成为开发者。 [img]http://files.jb51.net/file_images/article/201405/2014051914181023.jpg[/img] 再滑动右上角启用按钮。 [img]http://files.jb51.net/file_images/article/201405/2014051914181024.jpg[/img] 恭喜,你成功启用开发模式。   [b]自动回复[/b] 在上面的例子中,实现了一个发送“?”就能回复当前时间的功能。 效果如下: [img]http://files.jb51.net/file_images/article/201405/2014051914181025.png[/img] 至此,你的微信公众平台账号已经实现自动回复了。     [b]第三章 基础接口消息及事件[/b][b] [/b]   所有账号在申请之后,都将获得基础接口的权限,基础接口中将包括接收用户消息,向用户回复消息,接受事件推送等三种服务。 [img]http://files.jb51.net/file_images/article/201405/2014051914181026.jpg[/img] [b]接收用户消息[/b] 目前普通用户能向公众账号推送五种格式的消息:文本(包括表情)、语音、图片、视频、位置、链接。 下面就这五种分别详解如下: [b]1. 文本(包括表情)[/b] 发送文本及表情 [img]http://files.jb51.net/file_images/article/201405/2014051914181027.jpg[/img] [b]2. 图片[/b] 发送图片 [img]http://files.jb51.net/file_images/article/201405/2014051914181028.jpg[/img] [b]3. 语音[/b] 发送语音 [img]http://files.jb51.net/file_images/article/201405/2014051914181029.jpg[/img] [b]4. 视频[/b] 发送视频 [img]http://files.jb51.net/file_images/article/201405/2014051914181030.jpg[/img] [b]5. 位置[/b] 发送位置 [img]http://files.jb51.net/file_images/article/201405/2014051914181031.jpg[/img] [b]6. 链接[/b] 发送链接 [img]http://files.jb51.net/file_images/article/201405/2014051914181032.jpg[/img]   [b]向用户回复消息[/b] 目前普通公众账号能向用户推送六种格式的消息:文本、图文、音乐、图片、语音、视频。其中图文消息包括单条图文消息和多条图文消息,展示方式有一点点不同。 下面就这几种分别详解如下:【图片、语音、视频由于需要用到和高级接口相关的media_id,在本教程中暂不讨论。】 [b]1. 文本消息格式[/b] 回复文本 [img]http://files.jb51.net/file_images/article/201405/2014051914181033.png[/img] [b]2. 图文消息格式[/b] 2.1 单条图文消息 回复单条图文 [img]http://files.jb51.net/file_images/article/201405/2014051914181034.png[/img] 2.2 多图文消息 回复多图文 [img]http://files.jb51.net/file_images/article/201405/2014051914181035.png[/img] [b]3. 音乐消息[/b] 回复音乐消息 [img]http://files.jb51.net/file_images/article/201405/2014051914181036.png[/img]   [b]接收事件推送[/b] 目前用户在关注和取消关注,以及点击菜单的时候会自动向公众平台发送事件推送消息: 1. 关注事件 [img]http://files.jb51.net/file_images/article/201405/2014051914181037.jpg[/img] [img]http://files.jb51.net/file_images/article/201405/2014051914181038.png[/img] [b]第四章 微信公众平台PHP SDK[/b] 方倍工作室开发了微信公众平台的PHPSDK,集成了目前所有消息及事件的接收及发送,代码如下:
[u]复制代码[/u] 代码如下:
<?php /*     方倍工作室     http://www.cnblogs.com/txw1958/     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"];         if($this->checkSignature()){             echo $echoStr;             exit;         }     }     //检查签名     private function checkSignature()     {         $signature = $_GET["signature"];         $timestamp = $_GET["timestamp"];         $nonce = $_GET["nonce"];         $token = TOKEN;         $tmpArr = array($token, $timestamp, $nonce);         sort($tmpArr, SORT_STRING);         $tmpStr = implode($tmpArr);         $tmpStr = sha1($tmpStr);         if($tmpStr == $signature){             return true;         }else{             return false;         }     }     //响应消息     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->receiveText($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 = "欢迎关注方倍工作室 ";                 $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 = "方倍工作室提供互联网相关产品与服务。";                         break;                     default:                         $content = "点击菜单:".$object->EventKey;                         break;                 }                 break;             case "LOCATION":                 $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;                 break;             case "VIEW":                 $content = "跳转链接 ".$object->EventKey;                 break;             default:                 $content = "receive a new event: ".$object->Event;                 break;         }         $result = $this->transmitText($object, $content);         return $result;     }     //接收文本消息     private function receiveText($object)     {         switch ($object->Content)         {             case "文本":                 $content = "这是个文本消息";                 break;             case "图文":             case "单图文":                 $content = array();                 $content[] = array("Title"=>"单图文标题",  "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");                 break;             case "多图文":                 $content = array();                 $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");                 $content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");                 $content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");                 break;             case "音乐":                 $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");                 break;             default:                 $content = date("Y-m-d H:i:s",time());                 break;         }         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 receiveImage($object)     {         $content = array("MediaId"=>$object->MediaId);         $result = $this->transmitImage($object, $content);         return $result;     }     //接收位置消息     private function receiveLocation($object)     {         $content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;         $result = $this->transmitText($object, $content);         return $result;     }     //接收语音消息     private function receiveVoice($object)     {         if (isset($object->Recognition) && !empty($object->Recognition)){             $content = "你刚才说的是:".$object->Recognition;             $result = $this->transmitText($object, $content);         }else{             $content = array("MediaId"=>$object->MediaId);             $result = $this->transmitVoice($object, $content);         }         return $result;     }     //接收视频消息     private function receiveVideo($object)     {         $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>"");         $result = $this->transmitVideo($object, $content);         return $result;     }     //接收链接消息     private function receiveLink($object)     {         $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;         $result = $this->transmitText($object, $content);         return $result;     }     //回复文本消息     private function transmitText($object, $content)     {         $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 transmitImage($object, $imageArray)     {         $itemTpl = "<Image>     <MediaId><![CDATA[%s]]></MediaId> </Image>";         $item_str = sprintf($itemTpl, $imageArray['MediaId']);         $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[image]]></MsgType> $item_str </xml>";         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());         return $result;     }     //回复语音消息     private function transmitVoice($object, $voiceArray)     {         $itemTpl = "<Voice>     <MediaId><![CDATA[%s]]></MediaId> </Voice>";         $item_str = sprintf($itemTpl, $voiceArray['MediaId']);         $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[voice]]></MsgType> $item_str </xml>";         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());         return $result;     }     //回复视频消息     private function transmitVideo($object, $videoArray)     {         $itemTpl = "<Video>     <MediaId><![CDATA[%s]]></MediaId>     <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>     <Title><![CDATA[%s]]></Title>     <Description><![CDATA[%s]]></Description> </Video>";         $item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']);         $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[video]]></MsgType> $item_str </xml>";         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());         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 transmitMusic($object, $musicArray)     {         $itemTpl = "<Music>     <Title><![CDATA[%s]]></Title>     <Description><![CDATA[%s]]></Description>     <MusicUrl><![CDATA[%s]]></MusicUrl>     <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music>";         $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);         $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> $item_str </xml>";         $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());         return $result;     }     //日志记录     private function logger($log_content)     {         if(isset($_SERVER['HTTP_APPNAME'])){   //SAE             sae_set_display_errors(false);             sae_debug($log_content);             sae_set_display_errors(true);         }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL             $max_size = 10000;             $log_filename = "log.xml";             if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}             file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);         }     } } ?>
使用第一章同样方法,将上述代码另存为index.php,压缩成index.zip,或者直接点击下载,上传到SAE,你的公众账号就能接收及回复所有消息类型及事件通知了。 在公众账号中回复以下文字,你将得到和上一章一样的回复内容。
[u]复制代码[/u] 代码如下:
文本 图文 多图文 音乐 时间
还可以尝试以下操作,体验一下其他消息 发送一张图片给公众账号 发送一段语音给公众账号 发送一段视频给公众账号 发送位置信息给公众账号 发送收藏中的链接给公众账号第五章 微信公众平台开发模式原理分析  在体验了上一节的各种功能之后,我们只是知其然,这一节里面,将介绍在上面的基础上介绍微信公众平台收发消息机制及原理,这是知其所以然。 开发模式成为开发者时的消息校验原理 在开发者首次提交验证申请时,微信服务器将发送GET请求到填写的URL上,并且带上四个参数(signature、timestamp、nonce、echostr),开发者通过对签名(即signature)的效验,来判断此条消息的真实性。 此后,每次开发者接收用户消息的时候,微信也都会带上前面三个参数(signature、timestamp、nonce)访问开发者设置的URL,开发者依然通过对签名的效验判断此条消息的真实性。效验方式与首次提交验证申请一致。
参数 描述
signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp 时间戳
nonce 随机数
echostr 随机字符串
开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。 加密/校验流程如下: 1. 将token、timestamp、nonce三个参数进行字典序排序 2. 将三个参数字符串拼接成一个字符串进行sha1加密 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信启用接口是由代码中的checkSignature()函数来实现校验的。如果对这一原理难以理解,可以暂时不用深究,继续看下面。 成为开发者后消息收发时的原理 再来看下这个图,当用户发送一个“?”时,系统回复了一个时间 [img]http://files.jb51.net/file_images/article/201405/2014051914181025.png[/img] 这一原理的消息流程图如下所示。 [img]http://files.jb51.net/file_images/article/201405/2014051914181039.jpg[/img] 从上图可以看出,用户在发送一个?后,微信服务器将组装一个消息发送给我们自己的服务器,自己的服务器然后回复一个时间,并且将该时间也按一定的规则组装,回复给公众账号,公众账号再回复给用户,在这个收发过程中,发送方和接收方进行了调换(ToUserName和FromUserName值互换),收发都是以xml格式在后台进行传输的, 所以[b]掌握各种消息类型的收发就是进行微信公众平台开发的基础[/b]! 下面对前面所述的各种消息类型讲解其XML数据包的格式。   [b]各种收发消息的XML数据包分析[/b] [b]接收消息[/b] [b]1. 文本(包括表情)[/b] 发送文本及表情 [img]http://files.jb51.net/file_images/article/201405/2014051914181027.jpg[/img] 文字后台格式:
[u]复制代码[/u] 代码如下:
<xml>  <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName>  <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName>  <CreateTime>1359028446</CreateTime>  <MsgType><![CDATA[text]]></MsgType>  <Content><![CDATA[测试文字]]></Content>  <MsgId>5836982729904121631</MsgId> </xml>
表情后台格式
[u]复制代码[/u] 代码如下:
<xml><ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName> <CreateTime>1359044526</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[/::)/::~/::B/::|/:8-)]]></Content> <MsgId>5837051792978241864</MsgId> </xml>
XML格式讲解
[u]复制代码[/u] 代码如下:
ToUserName 消息接收方微信号,一般为公众平台账号微信号 FromUserName 消息发送方微信号 CreateTime 消息创建时间 MsgType 消息类型;文本消息为text Content 消息内容 MsgId 消息ID号
可以看出,文本和表情的消息类型均为文本 2. 图片 发送图片 [img]http://files.jb51.net/file_images/article/201405/2014051914181028.jpg[/img] 后台格式:
[u]复制代码[/u] 代码如下:
<xml><ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName> <CreateTime>1359028479</CreateTime> <MsgType><![CDATA[image]]></MsgType> <PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz/L4qjYtOibummHn90t1mnaibYiaR8ljyicF3MW7XX3BLp1qZgUb7CtZ0DxqYFI4uAQH1FWs3hUicpibjF0pOqLEQyDMlg/0]]></PicUrl> <MsgId>5836982871638042400</MsgId> <MediaId><![CDATA[PGKsO3LAgbVTsFYO7FGu51KUYa07D0C_Nozz2fn1z6VYtHOsF59PTFl0vagGxkVH]]></MediaId> </xml>
XML格式讲解
[u]复制代码[/u] 代码如下:
ToUserName 消息接收方微信号,一般为公众平台账号微信号 FromUserName 消息发送方微信号 CreateTime 消息创建时间 MsgType 消息类型;图片消息为image PicUrl 图片链接地址,可以用HTTP GET获取 MsgId 消息ID号
3. 语音 发送语音 [img]http://files.jb51.net/file_images/article/201405/2014051914181029.jpg[/img] 后台格式:
[u]复制代码[/u] 代码如下:
<xml><ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName> <CreateTime>1359028479</CreateTime> <MsgType><![CDATA[image]]></MsgType> <PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz/L4qjYtOibummHn90t1mnaibYiaR8ljyicF3MW7XX3BLp1qZgUb7CtZ0DxqYFI4uAQH1FWs3hUicpibjF0pOqLEQyDMlg/0]]></PicUrl> <MsgId>5836982871638042400</MsgId> <MediaId><![CDATA[PGKsO3LAgbVTsFYO7FGu51KUYa07D0C_Nozz2fn1z6VYtHOsF59PTFl0vagGxkVH]]></MediaId> </xml>
XML格式讲解
[u]复制代码[/u] 代码如下:
ToUserName 消息接收方微信号,一般为公众平台账号微信号 FromUserName 消息发送方微信号 CreateTime 消息创建时间 MsgType 消息类型;语音消息为voice MediaId 媒体ID Format 语音格式,这里为amr MsgId 消息ID号
附:AMR接口简介 全称Adaptive Multi-Rate,主要用于移动设备的音频,压缩比比较大,但相对其他的压缩格式质量比较差,由于多用于人声,通话,效果还是很不错的。 4. 视频 发送视频 [img]http://files.jb51.net/file_images/article/201405/2014051914181030.jpg[/img] 后台格式:
[u]复制代码[/u] 代码如下:
xml><ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName> <CreateTime>1359028186</CreateTime> <MsgType><![CDATA[video]]></MsgType> <MediaId><![CDATA[DBVFRIj29LB2hxuYpc0R6VLyxwgyCHZPbRj_IIs6YaGhutyXUKtFSDcSCPeoqUYr]]></MediaId> <ThumbMediaId><![CDATA[mxUJ5gcCeesJwx2T9qsk62YzIclCP_HnRdfTQcojlPeT2G9Q3d22UkSLyBFLZ01J]]></ThumbMediaId> <MsgId>5836981613212624665</MsgId> </xml>
XML格式讲解
[u]复制代码[/u] 代码如下:
ToUserName 消息接收方微信号,一般为公众平台账号微信号 FromUserName 消息发送方微信号 CreateTime 消息创建时间 MsgType 消息类型;视频消息为video MediaId 媒体ID ThumbMediaId 媒体缩略ID? MsgId 消息ID号
5. 位置 发送位置 [img]http://files.jb51.net/file_images/article/201405/2014051914181031.jpg[/img] 后台格式:
[u]复制代码[/u] 代码如下:
<xml> <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FLACFromUserName> <CreateTime>1359036619</CreateTime> <MsgType><![CDATA[location]]></MsgType> <Location_X>22.539968</Location_X> <Location_Y>113.954980</Location_Y> <Scale>16</Scale> <Label><![CDATA[中国广东省深圳市南山区华侨城深南大道9789号 邮政编码: 518057]]></Label> <MsgId>5837017832671832047</MsgId> </xml>
XML格式讲解
[u]复制代码[/u] 代码如下:
ToUserName 消息接收方微信号,一般为公众平台账号微信号  FromUserName 消息发送方微信号  CreateTime 消息创建时间  MsgType 消息类型,地理位置为location  Location_X 地理位置纬度  Location_Y 地理位置经度  Scale 地图缩放大小  Label 地理位置信息  MsgId 消息ID号
6. 链接 发送链接 [img]http://files.jb51.net/file_images/article/201405/2014051914181032.jpg[/img] 后台格式:
[u]复制代码[/u] 代码如下:
<xml> <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjl2LYdfTAM-oxDgB4XZcnc8]]></FromUserName> <CreateTime>1359709372</CreateTime> <MsgType><![CDATA[link]]></MsgType> <Title><![CDATA[微信公众平台开发者的江湖]]></Title> <Description><![CDATA[陈坤的微信公众号这段时间大火,大家..]]></Description> <Url><![CDATA[http://israel.duapp.com/web/photo.php]]></Url> <MsgId>5839907284805129867</MsgId> </xml>
XML格式讲解
[u]复制代码[/u] 代码如下:
ToUserName 消息接收方微信号,一般为公众平台账号微信号  FromUserName 消息发送方微信号  CreateTime 消息创建时间  MsgType 消息类型,链接为link  Title 图文消息标题  Description 图文消息描述  Url 点击图文消息跳转链接  MsgId 消息ID号
发送消息 只介绍三种格式的消息:文本、图文、音乐。其中图文消息包括单条图文消息和多条图文消息,展示方式有一点点不同。 1. 文本消息格式 回复文本 [img]http://files.jb51.net/file_images/article/201405/2014051914181040.jpg[/img] 后台格式:
[u]复制代码[/u] 代码如下:
<xml> <ToUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></ToUserName> <FromUserName><![CDATA[gh_680bdefc8c5d]]></FromUserName> <CreateTime>1359036631</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[【深圳】天气实况 温度:27℃ 湿度:59% 风速:东北风3级 11月03日 周日 27℃~23℃ 小雨 东北风4-5级 11月04日 周一 26℃~21℃ 阵雨 微风 11月05日 周二 27℃~22℃ 阴 微风]]></Content> <FuncFlag>0</FuncFlag> </xml>
XML格式讲解
[u]复制代码[/u] 代码如下:
FromUserName 消息发送方  ToUserName 消息接收方  CreateTime 消息创建时间  MsgType 消息类型,文本消息必须填写text  Content 消息内容,大小限制在2048字节,字段为空为不合法请求  FuncFlag 星标字段
2. 图文消息格式 2.1 单条图文消息 回复单条图文 [img]http://files.jb51.net/file_images/article/201405/2014051914181041.jpg[/img] [img]http://files.jb51.net/file_images/article/201405/2014051914181042.jpg[/img] 后台格式:
[u]复制代码[/u] 代码如下:
<xml>     <ToUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></ToUserName>     <FromUserName><![CDATA[gh_680bdefc8c5d]]></FromUserName>     <CreateTime>1359011899</CreateTime>     <MsgType><![CDATA[news]]></MsgType>     <Content><![CDATA[]]></Content>     <ArticleCount>1</ArticleCount>     <Articles>         <item>             <Title><![CDATA[[苹果产品信息查询]]></Title>             <Description><![CDATA[序列号:USE IMEI NUMBER IMEI号:358031058974471 设备名称:iPhone 5C 设备颜色: 设备容量: 激活状态:已激活 电话支持:未过期[2014-01-13] 硬件保修:未过期[2014-10-14] 生产工厂:中国]]>     </Description>             <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/banner.jpg]]></PicUrl>             <Url><![CDATA[]]></Url>         </item>     </Articles>     <FuncFlag>0</FuncFlag> </xml>
2.2 多图文消息 回复多图文 [img]http://files.jb51.net/file_images/article/201405/2014051914181043.jpg[/img] [img]http://files.jb51.net/file_images/article/201405/2014051914181044.jpg[/img] 后台数据格式
[u]复制代码[/u] 代码如下:
<xml>     <ToUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></ToUserName>     <FromUserName><![CDATA[gh_680bdefc8c5d]]></FromUserName>     <CreateTime>1359011829</CreateTime>     <MsgType><![CDATA[news]]></MsgType>     <Content><![CDATA[]]></Content>     <ArticleCount>5</ArticleCount>     <Articles>         <item>             <Title><![CDATA[【深圳】天气实况 温度:3℃ 湿度:43﹪ 风速:西南风2级]]></Title>             <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/banner.jpg]]></PicUrl>             <Url><![CDATA[]]></Url>         </item>         <item>             <Title><![CDATA[06月24日 周四 2℃~-7℃ 晴 北风3-4级转东南风小于3级]]></Title>             <Description><![CDATA[]]></Description>             <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/d00.gif]]></PicUrl>             <Url><![CDATA[]]></Url>         </item>         <item>             <Title><![CDATA[06月25日 周五 -1℃~-8℃ 晴 东南风小于3级转东北风3-4级]]></Title>             <Description><![CDATA[]]></Description>     <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/d00.gif]]></PicUrl>             <Url><![CDATA[]]></Url>         </item>         <item>             <Title><![CDATA[06月26日 周六 -1℃~-7℃ 多云 东北风3-4级转东南风小于3级]]></Title>             <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/d01.gif]]></PicUrl>             <Url><![CDATA[]]></Url>         </item>         <item>             <Title><![CDATA[06月27日 周日 0℃~-6℃ 多云 东南风小于3级转东北风3-4级]]></Title>             <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/d01.gif]]></PicUrl>             <Url><![CDATA[]]></Url>         </item>     </Articles>     <FuncFlag>0</FuncFlag> </xml>
XML格式讲解
[u]复制代码[/u] 代码如下:
FromUserName 消息发送方  ToUserName 消息接收方  CreateTime 消息创建时间  MsgType 消息类型,图文消息必须填写news  Content 消息内容,图文消息可填空  ArticleCount 图文消息个数,限制为10条以内  Articles 多条图文消息信息,默认第一个item为大图   Title 图文消息标题   Description 图文消息描述   PicUrl 图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80   Url 点击图文消息跳转链接 FuncFlag 星标字段
3. 音乐消息 回复音乐消息 [img]http://files.jb51.net/file_images/article/201405/2014051914181045.jpg[/img] 后台格式:
[u]复制代码[/u] 代码如下:
<xml>     <ToUserName><![CDATA[ollB4jqgdO_cRnVXk_wRnSywgtQ8]]></ToUserName>     <FromUserName><![CDATA[gh_b629c48b653e]]></FromUserName>     <CreateTime>1372310544</CreateTime>     <MsgType><![CDATA[music]]></MsgType>     <Music>         <Title><![CDATA[最炫民族风]]></Title>         <Description><![CDATA[凤凰传奇]]></Description>         <MusicUrl><![CDATA[http://zj189.cn/zj/download/music/zxmzf.mp3]]></MusicUrl>         <HQMusicUrl><![CDATA[http://zj189.cn/zj/download/music/zxmzf.mp3]]></HQMusicUrl>     </Music>     <FuncFlag>0</FuncFlag> </xml>
XML格式讲解
[u]复制代码[/u] 代码如下:
ToUserName     接收方帐号(收到的OpenID) FromUserName     开发者微信号 CreateTime     消息创建时间 MsgType          消息类型,此处为music     Title       音乐标题     Description 音乐描述     MusicUrl     音乐链接     HQMusicUrl     高质量音乐链接,WIFI环境优先使用该链接播放音乐 FuncFlag     位0x0001被标志时,星标刚收到的消息。
事件消息类型 目前用户在关注和取消关注,以及点击菜单的时候会自动向公众平台发送事件推送消息: 1. 关注事件
[u]复制代码[/u] 代码如下:
<xml>     <ToUserName><![CDATA[gh_b629c48b653e]]></ToUserName>     <FromUserName><![CDATA[ollB4jv7LA3tydjviJp5V9qTU_kA]]></FromUserName>     <CreateTime>1372307736</CreateTime>     <MsgType><![CDATA[event]]></MsgType>     <Event><![CDATA[subscribe]]></Event>     <EventKey><![CDATA[]]></EventKey> </xml>
2. 取消关注事件
[u]复制代码[/u] 代码如下:
<xml>     <ToUserName><![CDATA[gh_b629c48b653e]]></ToUserName>     <FromUserName><![CDATA[ollB4jqgdO_cRnVXk_wRnSywgtQ8]]></FromUserName>     <CreateTime>1372309890</CreateTime>     <MsgType><![CDATA[event]]></MsgType>     <Event><![CDATA[unsubscribe]]></Event>     <EventKey><![CDATA[]]></EventKey> </xml>
3. 菜单点击事件
[u]复制代码[/u] 代码如下:
<xml>     <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName>     <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName>     <CreateTime>1377886191</CreateTime>     <MsgType><![CDATA[event]]></MsgType>     <Event><![CDATA[CLICK]]></Event>     <EventKey><![CDATA[天气深圳]]></EventKey> </xml>
XML格式讲解
[u]复制代码[/u] 代码如下:
ToUserName     接收方微信号 FromUserName 发送方微信号,若为普通用户,则是一个OpenID CreateTime     消息创建时间 MsgType     消息类型,event Event     事件类型,subscribe(订阅)、unsubscribe(取消订阅)、CLICK(自定义菜单点击事件) EventKey 事件KEY值,与自定义菜单接口中KEY值对应
[b]第六章 开发天气预报功能[/b] 这一章里,我们来快速开发天气预报功能、我们使用方倍工作室的相应接口来实现。下面代码实现了这样该功能。
[u]复制代码[/u] 代码如下:
<?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"];         if($this->checkSignature()){             echo $echoStr;             exit;         }     }     private function checkSignature()     {         $signature = $_GET["signature"];         $timestamp = $_GET["timestamp"];         $nonce = $_GET["nonce"];         $token = TOKEN;         $tmpArr = array($token, $timestamp, $nonce);         sort($tmpArr, SORT_STRING);         $tmpStr = implode($tmpArr);         $tmpStr = sha1($tmpStr);         if($tmpStr == $signature){             return true;         }else{             return false;         }     }     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->receiveText($postObj);                     break;             }             $this->logger("T ".$result);             echo $result;         }else {             echo "";             exit;         }     }     private function receiveEvent($object)     {         $conte
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部