微信公众平台开发(十二) 发送客服消息
来源:http://www.cnblogs.com/mchina/p/3711217.html<p></p><div><br></div><div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">一、简介</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">当用户主动发消息给公众号的时候(包括发送信息、点击自定义菜单、订阅事件、扫描二维码事件、支付成功事件、用户维权),微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改为48小时)可以调用客服消息接口,通过POST一个JSON数据包来发送消息给普通用户,在48小时内不限制发送次数。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">二、思路分析</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">官方文档中只提供了一个发送客服消息的接口,开发者只要POST一个特定的JSON数据包即可实现消息回复。在这里,我们打算做成一个简单的平台,可以记录用户消息,并且用网页表格的形式显示出来,然后可以对消息进行回复操作。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">首先,我们使用数据库记录用户主动发送过来的消息,然后再提取出来展示到页面,针对该消息,进行回复。这里我们只讨论文本消息,关于其他类型的消息,大家自行研究。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">三、记录用户消息</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>3.1 创建数据表</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">创建一张数据表tbl_customer 来记录用户消息。</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 128, 128);">----</span><span style="line-height: 1.8; color: rgb(0, 128, 128);"> 表的结构 `tbl_customer`</span><span style="line-height: 1.8; color: rgb(0, 128, 128);">
--
</span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);">CREATE</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">TABLE</span><span style="line-height: 1.8;"> `tbl_customer` (
`id` </span><span style="line-height: 1.8; color: rgb(0, 0, 255);">bigint</span>(<span style="line-height: 1.8; color: rgb(128, 0, 0); font-weight: bold;">20</span>) unsigned <span style="line-height: 1.8; color: rgb(128, 128, 128);">NOT</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">NULL</span> AUTO_INCREMENT COMMENT <span style="line-height: 1.8; color: rgb(255, 0, 0);">'</span><span style="line-height: 1.8; color: rgb(255, 0, 0);">//消息ID</span><span style="line-height: 1.8; color: rgb(255, 0, 0);">'</span><span style="line-height: 1.8;">,
`from_user` </span><span style="line-height: 1.8; color: rgb(0, 0, 255);">char</span>(<span style="line-height: 1.8; color: rgb(128, 0, 0); font-weight: bold;">50</span>) <span style="line-height: 1.8; color: rgb(128, 128, 128);">NOT</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">NULL</span> COMMENT <span style="line-height: 1.8; color: rgb(255, 0, 0);">'</span><span style="line-height: 1.8; color: rgb(255, 0, 0);">//消息发送者</span><span style="line-height: 1.8; color: rgb(255, 0, 0);">'</span><span style="line-height: 1.8;">,
`message` </span><span style="line-height: 1.8; color: rgb(0, 0, 255);">varchar</span>(<span style="line-height: 1.8; color: rgb(128, 0, 0); font-weight: bold;">200</span>) <span style="line-height: 1.8; color: rgb(128, 128, 128);">NOT</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">NULL</span> COMMENT <span style="line-height: 1.8; color: rgb(255, 0, 0);">'</span><span style="line-height: 1.8; color: rgb(255, 0, 0);">//消息体</span><span style="line-height: 1.8; color: rgb(255, 0, 0);">'</span><span style="line-height: 1.8;">,
`time_stamp` </span><span style="line-height: 1.8; color: rgb(0, 0, 255);">datetime</span> <span style="line-height: 1.8; color: rgb(128, 128, 128);">NOT</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">NULL</span> COMMENT <span style="line-height: 1.8; color: rgb(255, 0, 0);">'</span><span style="line-height: 1.8; color: rgb(255, 0, 0);">//消息发送时间</span><span style="line-height: 1.8; color: rgb(255, 0, 0);">'</span><span style="line-height: 1.8;">,
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">PRIMARY</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">KEY</span><span style="line-height: 1.8;"> (`id`),
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">KEY</span><span style="line-height: 1.8;"> `from_user` (`from_user`)
) ENGINE</span><span style="line-height: 1.8; color: rgb(128, 128, 128);">=</span>MyISAM<span style="line-height: 1.8; color: rgb(0, 0, 255);">DEFAULT</span> CHARSET<span style="line-height: 1.8; color: rgb(128, 128, 128);">=</span>utf8 ;</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>3.2 创建sql.func.php 文件</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">创建 _query($_sql) {} 函数,来执行INSERT 操作。</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 0, 255);">function</span> _query(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$_sql</span><span style="line-height: 1.8;">){
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">if</span>(!<span style="line-height: 1.8; color: rgb(128, 0, 128);">$_result</span> = <span style="line-height: 1.8; color: rgb(0, 128, 128);">mysql_query</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$_sql</span><span style="line-height: 1.8;">)){
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">exit</span>('SQL执行失败'.<span style="line-height: 1.8; color: rgb(0, 128, 128);">mysql_error</span><span style="line-height: 1.8;">());
}
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">return</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$_result</span><span style="line-height: 1.8;">;
}</span></pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong><span style="line-height: 1.5;">3.3 创建记录消息的函数文件record_message.func.inc.php</span></strong></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">引入数据库处理函数</span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);">require_once</span> 'sql.func.php'<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">function</span> _record_message(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$fromusername</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$keyword</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$date_stamp</span><span style="line-height: 1.8;">){
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">调用_query()函数</span>
_query("INSERT INTO tbl_customer(from_user,message,time_stamp) VALUES('<span style="line-height: 1.8; color: rgb(128, 0, 128);">$fromusername</span>','<span style="line-height: 1.8; color: rgb(128, 0, 128);">$keyword</span>','<span style="line-height: 1.8; color: rgb(128, 0, 128);">$date_stamp</span>')"<span style="line-height: 1.8;">);
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>3.4 处理并记录文本消息</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">A. 引入回复文本的函数文件,引入记录消息的函数文件</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">引入回复文本的函数文件</span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);">require_once</span> 'responseText.func.inc.php'<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">引入记录消息的函数文件</span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);">require_once</span> 'record_message.func.inc.php';</pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">B. 记录消息入数据库,并返回给用户刚才发送的消息,在这里,你可以修改成其他的文本,比如:“你好,消息已收到,我们会尽快回复您!” 等等。</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"> <span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">处理文本消息函数</span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);">public</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">function</span> handleText(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$postObj</span><span style="line-height: 1.8;">)
{
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">获取消息发送者,消息体,时间戳</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$fromusername</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$postObj</span>-><span style="line-height: 1.8;">FromUserName;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$keyword</span> = <span style="line-height: 1.8; color: rgb(0, 128, 128);">trim</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$postObj</span>-><span style="line-height: 1.8;">Content);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$date_stamp</span> = <span style="line-height: 1.8; color: rgb(0, 128, 128);">date</span>('Y-m-d H:i:s'<span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">if</span>(!<span style="line-height: 1.8; color: rgb(0, 0, 255);">empty</span>( <span style="line-height: 1.8; color: rgb(128, 0, 128);">$keyword</span><span style="line-height: 1.8;"> ))
{
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">调用_record_message()函数,记录消息入数据库</span>
_record_message(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$fromusername</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$keyword</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$date_stamp</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$contentStr</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$keyword</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">调用_response_text()函数,回复发送者消息</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$resultStr</span> = _response_text(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$postObj</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$contentStr</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">echo</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$resultStr</span><span style="line-height: 1.8;">;
}</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">else</span><span style="line-height: 1.8;">{
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">echo</span> "Input something..."<span style="line-height: 1.8;">;
}
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">四、网页展示用户消息</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">我们的最终效果大概如下所示,主要的工作在“信息管理中心”这块,其他的页面布局等等,不在这里赘述了,只讲解消息展示这块。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/i/370046/201405/061452129179503.jpg" alt="" width="900" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>4.1 具体实施</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">引入数据库操作文件,执行分页模块,执行数据库查询,将查询出来的结果赋给$_result 供下面使用。</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">引入数据库操作文件</span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);">require_once</span> 'includes/sql.func.php'<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">分页模块</span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);">global</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$_pagesize</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$_pagenum</span><span style="line-height: 1.8;">;
_page(</span>"SELECT id FROM tbl_customer",15); <span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">第一个参数获取总条数,第二个参数,指定每页多少条</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$_result</span> = _query("SELECT * FROM tbl_customer ORDER BY id DESC LIMIT <span style="line-height: 1.8; color: rgb(128, 0, 128);">$_pagenum</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$_pagesize</span>");</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">将$_result 遍历出来,依次插入表格中。</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">form</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">table </span><span style="line-height: 1.8; color: rgb(255, 0, 0);">cellspacing</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="1"</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">tr</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">th</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>消息ID<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">th</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">th</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>发送者<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">th</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">th</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>消息体<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">th</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">th</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>消息时间<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">th</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">th</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>操作<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">th</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">tr</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><?</span><span style="line-height: 1.8; color: rgb(255, 0, 255);">php
while(!!$_rows = _fetch_array_list($_result)){
$_html = array();
$_html['id'] = $_rows['id'];
$_html['from_user'] = $_rows['from_user'];
$_html['message'] = $_rows['message'];
$_html['time_stamp'] = $_rows['time_stamp'];
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">?></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">tr</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">td</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"><?</span><span style="line-height: 1.8; color: rgb(255, 0, 255);">php echo $_html['id']</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">?></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">td</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">td</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"><?</span><span style="line-height: 1.8; color: rgb(255, 0, 255);">php echo $_html['from_user']</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">?></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">td</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">td</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"><?</span><span style="line-height: 1.8; color: rgb(255, 0, 255);">php echo $_html['message']</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">?></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">td</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">td</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"><?</span><span style="line-height: 1.8; color: rgb(255, 0, 255);">php echo $_html['time_stamp']</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">?></span><span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">td</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">td</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">a </span><span style="line-height: 1.8; color: rgb(255, 0, 0);">href</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="reply.php?fromusername=<?php echo $_html['from_user']?>&message=<?php echo $_html['message']?>"</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">input </span><span style="line-height: 1.8; color: rgb(255, 0, 0);">type</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="button"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> value</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="回复"</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">/></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">a</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">td</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">tr</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><?</span><span style="line-height: 1.8; color: rgb(255, 0, 255);">php
}
_free_result($_result);
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">?></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">table</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">form</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>说明:</strong>在每条消息后,都有一个“回复”操作,点击该按钮,向reply.php文件中传入fromusername和用户发送的消息,为回复用户消息做准备。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">五、消息回复</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>5.1 创建客服消息回复函数文件customer.php</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">微信发送客服消息的接口URL如下:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;">https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN</pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">需要POST的JSON数据包格式如下:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8;">{
"touser":"OPENID",
"msgtype":"text",
"text":
{
"content":"Hello World"
}
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">所以,根据上面的提示,我们编写处理函数 _reply_customer($touser,$content),调用的时候,传入touser和需要回复的content,即可发送客服消息。</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 0, 255);">function</span> _reply_customer(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$touser</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$content</span><span style="line-height: 1.8;">){
</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">更换成自己的APPID和APPSECRET</span>
<span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPID</span>="wxef78f22f877db4c2"<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPSECRET</span>="3f3aa6ea961b6284057b8170d50e2048"<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$TOKEN_URL</span>="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPID</span>."&secret=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPSECRET</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$json</span>=<span style="line-height: 1.8; color: rgb(0, 128, 128);">file_get_contents</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$TOKEN_URL</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span>=json_decode(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$json</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ACC_TOKEN</span>=<span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span>-><span style="line-height: 1.8;">access_token;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$data</span> = '<span style="line-height: 1.8;">{
"touser":"</span>'.<span style="line-height: 1.8; color: rgb(128, 0, 128);">$touser</span>.'<span style="line-height: 1.8;">",
"msgtype":"text",
"text":
{
"content":"</span>'.<span style="line-height: 1.8; color: rgb(128, 0, 128);">$content</span>.'<span style="line-height: 1.8;">"
}
}</span>'<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$url</span> = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ACC_TOKEN</span><span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span> = https_post(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$url</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$data</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$final</span> = json_decode(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">return</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$final</span><span style="line-height: 1.8;">;
}
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">function</span> https_post(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$url</span>,<span style="line-height: 1.8; color: rgb(128, 0, 128);">$data</span><span style="line-height: 1.8;">)
{
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span> =<span style="line-height: 1.8;"> curl_init();
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span>, CURLOPT_URL, <span style="line-height: 1.8; color: rgb(128, 0, 128);">$url</span><span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span>, CURLOPT_SSL_VERIFYPEER, <span style="line-height: 1.8; color: rgb(0, 0, 255);">FALSE</span><span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span>, CURLOPT_SSL_VERIFYHOST, <span style="line-height: 1.8; color: rgb(0, 0, 255);">FALSE</span><span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span>, CURLOPT_POST, 1<span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span>, CURLOPT_POSTFIELDS, <span style="line-height: 1.8; color: rgb(128, 0, 128);">$data</span><span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span>, CURLOPT_RETURNTRANSFER, 1<span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span> = curl_exec(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">if</span> (curl_errno(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span><span style="line-height: 1.8;">)) {
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">return</span> 'Errno'.curl_error(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span><span style="line-height: 1.8;">);
}
curl_close(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$curl</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">return</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span><span style="line-height: 1.8;">;
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">下面,我们就将上面写好的函数引入到消息回复页面,实现发送客服消息的功能。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>5.2 点击“回复”按钮,带上fromusername和message参数跳转到reply.php。</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/i/370046/201405/061421507134978.jpg" alt="" width="900" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>5.3 reply.php 页面显示</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/i/370046/201405/061425282139425.jpg" alt="" width="900" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>5.4 reply.php文件分析</strong></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 128, 0);">//</span><span style="line-height: 1.8; color: rgb(0, 128, 0);">引入回复消息的函数文件</span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);">require_once</span> '../customer.php';</pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">form表单提交到relpy.php本身,带有action=relpy.</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">form </span><span style="line-height: 1.8; color: rgb(255, 0, 0);">method</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="post"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> action</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="reply.php?action=reply"</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">dl</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">dd</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">strong</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>收件人:<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">strong</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">input </span><span style="line-height: 1.8; color: rgb(255, 0, 0);">type</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="text"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> name</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="tousername"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> class</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="text"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> value</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="<?php echo $from_username?>"</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">/></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">dd</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">dd</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">strong</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>原消息:<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">strong</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">input </span><span style="line-height: 1.8; color: rgb(255, 0, 0);">type</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="text"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> name</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="message"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> class</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="text"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> value</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="<?php echo $message?>"</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">/></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">dd</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">dd</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">span</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">strong</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>内 容:<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">strong</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">span</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">textarea </span><span style="line-height: 1.8; color: rgb(255, 0, 0);">rows</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="5"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> cols</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="34"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> name</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="content"</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">textarea</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">dd</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">dd</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">><</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">input </span><span style="line-height: 1.8; color: rgb(255, 0, 0);">type</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="submit"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> class</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="submit"</span><span style="line-height: 1.8; color: rgb(255, 0, 0);"> value</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">="回复消息"</span> <span style="line-height: 1.8; color: rgb(0, 0, 255);">/></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">dd</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">dl</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span>
<span style="line-height: 1.8; color: rgb(0, 0, 255);"></</span><span style="line-height: 1.8; color: rgb(128, 0, 0);">form</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">></span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">action=reply 动作处理。</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="line-height: 1.8; color: rgb(0, 0, 255);">if</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$_GET</span>['action'] == "reply"<span style="line-height: 1.8;">){
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$touser</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$_POST</span>['tousername'<span style="line-height: 1.8;">];
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$content</span> = <span style="line-height: 1.8; color: rgb(128, 0, 128);">$_POST</span>['content'<span style="line-height: 1.8;">];
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span> = _reply_customer(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$touser</span>, <span style="line-height: 1.8; color: rgb(128, 0, 128);">$content</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">if</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span>->errcode == "0"<span style="line-height: 1.8;">){
_location(</span>'消息回复成功!', 'index.php'<span style="line-height: 1.8;">);
}
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>说明:</strong>POST方式获取touser, content,然后调用_reply_customer($touser, $content)方法处理,处理成功,则弹出“消息回复成功!”,然后跳转到index.php页面,完成发送客服消息过程。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">六、测试</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>6.1 微信用户发送消息</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/i/370046/201405/061518245574816.png" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>6.2 平台消息管理</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/i/370046/201405/061519049482288.jpg" alt="" width="900" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>6.3 发送客服消息</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/i/370046/201405/061519428233559.jpg" alt="" width="900" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/i/370046/201405/061521476354024.png" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">再次发送客服消息</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/i/370046/201405/061520495103767.jpg" alt="" width="900" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/i/370046/201405/061516515428331.png" alt="" style="border: 0px; max-width: 900px;"> </p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">发送客服消息测试成功!</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">七、代码获取</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><a href="http://files.cnblogs.com/mchina/customer.rar" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">http://files.cnblogs.com/mchina/customer.rar</a></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">八、总结</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">微信发送客服消息本身很简单,只需POST一个JSON数据包到指定接口URL即可。这里我们进行了扩展,写成一个简单的平台,方便企业的管理。还有很多需要补充和改进的地方,例如,记录客服发送的消息;将相同用户的消息记录成一个集合;实现其他格式的消息回复等,有待读者自行思考开发。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"> </p><hr style="color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>David Camp</strong></span></p><ul style="margin-left: 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><li style="margin-left: 0px;">业务合作,请联系作者QQ:562866602</li><li style="margin-left: 0px;">我的微信号:mchina_tang</li><li style="margin-left: 0px;">给我写信:mchina_tang@qq.com</li></ul><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong><strong><strong>我们永远相信,分享是一种美德 | </strong></strong>We Believe, Great People Share Knowledge...</strong></span></p></div>
页:
[1]