基于PHP实现微信小程序客服消息功能

2025-05-29 0 68

项目说明:

本项目是一个简单微信小程序客服消息类,实现客服消息相关功能。官方给的php示例有误,这里就不再吐槽了。

本示例是采用开发者服务器,没有采用云调用的形式。

官方文档:

客服消息指南

客服消息服务端

适用场景

基于PHP实现微信小程序客服消息功能

客户消息流程图

基于PHP实现微信小程序客服消息功能

使用步骤

1、开启客服消息

https://mp.weixin.qq.com/wxam…

登录-开发-开发设置-消息推送

[](https://raw.githubusercontent…

点击“启动”

[](https://raw.githubusercontent…

URL(服务器地址):填开发者服务器对应的url,如https://xxxxxx/demo.php

token(令牌):这个随便填,要求3-32位。

encodingaeskey(消息加密密钥):这个点击“随机生成”即可。

消息加密方式:可以根据自己需要选择,本例选择”兼容模式“。

数据格式:json相对于xml来说,从压缩效率及传输效率更具优势,这里我们选json。

注意:以上操作完后先不要提交,等配置好开发者服务端后再提交。

2、配置开发者服务端

检验signature的php示例代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
$signature = $_get["signature"];

$timestamp = $_get["timestamp"];

$nonce = $_get["nonce"];

$echostr=$_get["echostr"];

$token = token;//这里改成你第一步操作时填写的token

$tmparr = array($token, $timestamp, $nonce);

sort($tmparr, sort_string);

$tmpstr = implode( $tmparr );

$tmpstr = sha1( $tmpstr );

if ($tmpstr == $signature ) {

return $echostr;

} else {

return false;

}

官方示例没有返回 $echostr ,这个检验开发者服务端是否成功的关键,必须返回。

3、提交消息推送配置

如果没有报错,证明配置成功。

基于PHP实现微信小程序客服消息功能

4、开发者服务端demo

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72
<?php

//验证signature

//$signature = $_get["signature"];

//$timestamp = $_get["timestamp"];

//$nonce = $_get["nonce"];

//$echostr=$_get["echostr"];

//

//$token = token;//这里改成你第一步操作时填写的token

//$tmparr = array($token, $timestamp, $nonce);

//sort($tmparr, sort_string);

//$tmpstr = implode( $tmparr );

//$tmpstr = sha1( $tmpstr );

//

//if ($tmpstr == $signature ) {

// return $echostr;

//} else {

// return false;

//}

include_once './xcxmsg.php';

$xcxmsg = new xcxmsg();

$poststr = file_get_contents('php://input');

if (!$poststr)

return false;

$postarr = json_decode($poststr, true);

if (!isset($postarr['msgtype']) || !isset($postarr['fromusername']))

return false;

$data = ["touser" => $postarr['fromusername']];

$accesstoken = $xcxmsg->getaccesstoken();

$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accesstoken;

switch ($postarr['msgtype']) {

case "text":

//如用户发送的是文字信息,这里处理

//回复图文链接,也可以回复别的类型,根据需要

$data['msgtype'] = "link";

$data['link'] = [

"title" => "hello",

"description" => "is really a happy day",

"url" => "link_url",//连接url

"thumb_url" =>"thumb_url" //图片url

];

$json = json_encode($data, json_unescaped_unicode);

$xcxmsg->curl($json, $url);

break;

case "image": //如用户发送图片消息,进入这里

//服务端回复 图片,也可以回复别的类型,根据需要

$data['msgtype'] = "image";

$data['image'] = ['media_id' => 'media_id值']; // 执行 $xcxmsg->upload($accesstoken)返回的 media_id

$json = json_encode($data, json_unescaped_unicode);

$xcxmsg->curl($json, $url);

case "miniprogrampage":

//如用户发送小程序卡片,进入这里

//这里服务端回复小卡片,也可以回复别的类型,根据需要

$data['msgtype'] = "miniprogrampage";

$data['miniprogrampage'] = [

"title" => "title",

"pagepath" => "pages/index/index",

"thumb_media_id" => "media_id值"];// 执行 $xcxmsg->upload($accesstoken)返回的 media_id

$json = json_encode($data, json_unescaped_unicode);

$xcxmsg->curl($json, $url);

break;

case "event":

//如用户进入会话事件

//这里可以回复文本

$data['msgtype'] = "text";

$data['text'] = [

"content" => "hello world",

];

$json = json_encode($data, json_unescaped_unicode);

$xcxmsg->curl($json, $url);

break;

default:

}

5、小程序前端

在需要的地方添加以下代码:

?

1
<button open-type="contact" >客服消息</button>

用微信开发工具的预览,生成二维码,扫描测试是否成功。

项目地址:https://github.com/guyan0319/…

总结

以上所述是小编给大家介绍的基于php实现微信小程序客服消息功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://segmentfault.com/a/1190000020030043

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 基于PHP实现微信小程序客服消息功能 https://www.kuaiidc.com/92455.html

相关文章

发表评论
暂无评论