Home > PHP Framework > Swoole > Introducing swoole asynchronous group sending template messages

Introducing swoole asynchronous group sending template messages

coldplay.xixi
Release: 2021-03-22 10:41:14
forward
3049 people have browsed it

Introducing swoole asynchronous group sending template messages

1、用的是TP5.1的框架,swoole分成一个客户端发送接收消息,一个服务器负责处理信息

  服务端代码,服务器要先安装swoole拓展,用 php server.php 启动进程监听

推荐(免费):swoole

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

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

<?php

namespace think;

date_default_timezone_set(&#39;Asia/Shanghai&#39;);

// 加载基础文件

require_once __DIR__ . &#39;/thinkphp/base.php&#39;;

 

// 支持事先使用静态方法设置Request对象和Config对象

 

// 执行应用并响应

//Container::get(&#39;app&#39;)->run()->send();

 

//require_once __DIR__ . '/../../../thinkphp/helper.php';

use think\cache\driver\Redis;

//use think\Controller;

use think\Db;

 

 

class Swoole

{

    const errcode = array(

        43004 => '需要接收者关注',

        40037 => '无效模板',

        40003 => '需要接收者关注',

        43005 => '需要好友关系',

        43019 => '需要将接收者从黑名单中移除',

        44001 => '多媒体文件为空',

        44002 => 'POST 的数据包为空',

        44003 => '图文消息内容为空',

        44004 => '文本消息内容为空',

        45001 => '多媒体文件大小超过限制',

        45002 => '消息内容超过限制',

        45003 => '标题字段超过限制',

        45004 => '描述字段超过限制',

        45005 => '链接字段超过限制',

        45006 => '图片链接字段超过限制',

        45007 => '语音播放时间超过限制',

        45008 => '图文消息超过限制',

        45009 => '接口调用超过限制',

        45010 => '创建菜单个数超过限制',

        45011 => 'API 调用太频繁,请稍候再试',

    );

    private $serv;

    private $redis;

    private $conn = [

        // 数据库类型

        'type'            => 'mysql',

        // 服务器地址

        'hostname'        => '',

        // 数据库名

        'database'        => '',

        // 用户名

        'username'        => '',

        // 密码

        'password'        => '',

        // 端口

        'hostport'        => '3306',

        // 连接dsn

        'dsn'             => '',

        // 数据库连接参数

        'params'          => [],

        // 数据库编码默认采用utf8

        'charset'         => 'utf8',

        // 数据库表前缀

        'prefix'          => 'shd_',

        // 数据库调试模式

        'debug'           => true,

        // 数据集返回类型

        'resultset_type'  => 'array',

        // 自动写入时间戳字段

        'auto_timestamp'  => false,

        // 时间字段取出后的默认时间格式

        'datetime_format' => 'Y-m-d H:i:s',

        // 是否需要进行SQL性能分析

        'sql_explain'     => false,

        // Builder类

        'builder'         => '',

        // Query类

        'query'           => '\\think\\db\\Query',

        // 是否需要断线重连

        'break_reconnect' => false,

        // 断线标识字符串

        'break_match_str' => [],

    ];

 

    //初始化配置,监听端口

    public function __construct()

    {

        //redis

        $this->redis = new Redis();

 

        $this->serv = new \swoole_server("0.0.0.0", 9501);

        $this->serv->set(array(

            'worker_num' => 2, //一般设置为服务器CPU数的1-4倍

            'daemonize' => 1, //以守护进程执行

            'max_request' => 10000,

            'dispatch_mode' => 2,

            'task_worker_num' => 8, //task进程的数量

            "task_ipc_mode " => 3, //使用消息队列通信,并设置为争抢模式

            "log_file" => "taskqueueu.log" ,//日志

        ));

        $this->serv->on('Receive'array($this'onReceive'));

        // bind callback

        $this->serv->on('Task'array($this'onTask'));

        $this->serv->on('Finish'array($this'onFinish'));

        $this->serv->start();

    }

     

   //接收客户端的请求并响应

    public function onReceive(\swoole_server $serv$fd$from_id$data)

    {

        echo "Get Message From Client {$fd}:{$data}\n";

 

        $serv->send($fd'发送任务已建立,正在发送,请稍后查看发送记录');

 

        // send a task to task worker.

        $serv->task($data);//投递任务

    }

 

    public function onTask($serv$task_id$from_id$data)

    {

        echo "Task {$task_id} task\n";

        $array = json_decode($data, true);

 

       $success = 0;

        $fail = 0;

        $log '';

 

        $access_token $array['access_token'];

 

        $openid_list $this->redis->sMembers($array['appid'].'users');//从redis取出要批量发送的openid

 

        $fields = json_decode($array['data'],true);

        $send_data array();

        

        $start = time();

        //模板消息

        foreach ($openid_list as $openid) {

            $template array(

                'touser' => $openid,

                'template_id' => $array['tem_id'],

                'url' => $array['url'],

                'topcolor' => "#000000",

                'data' => $send_data,

            );

 

 

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

            $res $this->send_post($url$template);

            $res_arr = json_decode($res, true);

            if ($res_arr['errcode'] == 0){

                ++ $success;

            }else{

                ++ $fail;

                $log = self::errcode[$res_arr['errcode']];

            }

 

        }

        $result array('success'=>$success,'fail'=>$fail,'tem_id'=>$array['tem_id'],'uid'=>$array['uid'],'data'=>$array['data'],'url'=>$array['url'],'log'=>$log,'start'=>$start);

        return json_encode($result);

 

    }

 

    //任务执行完自动回调结束方法

    public function onFinish($serv$task_id$data)

    {

        $array = json_decode($data,true);

        $fields = json_decode($array['data'],true);

 

        //获取当前模板

        $list =  Db::connect($this->conn)->name('wechat_template')->where('template_id',$array['tem_id'])->where('uid',$array['uid'])->find();

 

        $new_field $list['field'];

         

        $insert['template_id'] = $array['tem_id'];

        $insert['success'] = $array['success'];

        $insert['fail'] = $array['fail'];

        $insert['url'] = $array['url'];

        $insert['log'] = $array['log'];

        $insert['create_time'] = date('Y-m-d H:i:s',$array['start']);

        $insert['finish_time'] = date('Y-m-d H:i:s');

 

        Db::connect($this->conn)->name('wechat_template_log')->insert($insert);

        echo "Task{$data} {$task_id} finish\n";

 

    }

 

    function send_post($url$post_data) {

        $postdata=json_encode($post_data,JSON_UNESCAPED_UNICODE);

        $options array(

            'http' => array(

                'method' => 'POST',

                'header' => 'Content-type:application/x-www-form-urlencoded',

                'content' => $postdata,

//            'protocol_version' => 1.1,

//            'header' => [

//                'Connection: close',

//            ],

                'timeout' => 2 // 超时时间(单位:s)

            )

        );

        $context = stream_context_create($options);

        $result file_get_contents($url, false, $context);

 

        return $result;

    }

 

 

 

}

 

$server new Swoole();

Copy after login

2、客户端请求,可以通过api访问

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

function send_tem_to(){

        $type = input('type'); // 0 按人头算 1 按标签算 2 全部粉丝

        $target = input('target/s');

        $field = input('fields/s');

        $tem_id = input('tem_id');//模板ID,字符串

        $url = input('url','');

 

        $client new \swoole_client(SWOOLE_SOCK_TCP);//创建同步TCP

        if (!$client->connect('127.0.0.1', 9501, 0.5))//链接

        {

            exit("connect failed. Error: {$client->errCode}\n");

        }

 

 

            $client->send(json_encode(array('appid'=>$this->appid,'uid'=>$this->uid,'tem_id'=>$tem_id,'data'=>$field))); //发送请求

            $rec $client->recv();//接收返回数据

            $client->close();//关闭链接

         

    }

Copy after login

The above is the detailed content of Introducing swoole asynchronous group sending template messages. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template