服务端代码:
<?php
$server = new Swoole\Server('127.0.0.1', 9503);
$server->set([
'worker_num' => 1,
'daemonize' => false, // 守护进程,开启后,关闭SSH面也可进行
'log_file' => 'swoole_task.log',
'task_worker_num' => 200 // task进程数量
]);
// 主进程启动时回调函数
$server->on('start', function ($server) {
echo "服务端启动:" . date("Y-m-d H:i:s") . PHP_EOL;
});
// 收到信息时执行
$server->on('receive', function ($server, $fd, $reactor_id, $data) {
echo "收到数据" . PHP_EOL;
$data = json_decode($data, true);
// $user = Db::name('wx_user')->where('subscribe', 1)->column('openid');
$user = ['oKVaUwrj986PE2hq1jgVQ7KO7h8s'];
foreach ($user as $key => $value) {
$info = [
'touser' => 'oKVaUwrj986PE2hq1jgVQ7KO7h8s',
'template_id' => 'U2rxKg6z5Q8TU-WWYoRkiifwoS955H2EqUZqn4efcnc',
'url' => 'www.baidu.com',
'data' => [
'first' => [
'value' => '标题',
'color' => '#173177',
],
'keyword1' => [
'value' => '项目名称',
'color' => '#CC3399',
],
'keyword2' => [
'value' => '最新状态',
'color' => '#CC3399',
],
'keyword3' => [
'value' => date('Y年m月d日 H:i:s'),
'color' => '#CC3399',
],
'remark' => [
'value' => '备注',
'color' => '#FF0000',
]
]
];
$info = json_encode($info);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $data['access_token'];
// 投递异步任务
$server->task([
'url' => $url,
'info' => $info
]);
}
});
// 处理异步任务
$server->on('task', function ($server, $task_id, $reactor_id, $data) {
echo "开始处理异步";
$res = httpRequest($data['url'], $data['info']);
echo $res . PHP_EOL;
$server->finish('请求回调方法');
});
// 异步任务完成通知回调
$server->on('finish', function ($server, $task_id, $data) {
echo "完成回调" . date("Y-m-d H:i:s") . ':' . $data;
});
// 建立连接
$server->on('connect', function ($server, $fd, $reactor_id) {
echo "客户端连接成功:" . date("Y-m-d H:i:s") . PHP_EOL;
});
// 关闭连时回调函数
$server->on('close', function ($server, $fd, $reactor_id) {
echo "客户端关闭连接:" . date("Y-m-d H:i:s") . PHP_EOL;
});
$server->start();
function httpRequest($url, $data)
{
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $data,
// 'protocol_version' => 1.1,
// 'header' => [
// 'Connection: close',
// ],
'timeout' => 2 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
客户端:
<?php
client();
/**
* 请求服务端
*/
function client($value = '')
{
$client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
$ret = $client->connect("127.0.0.1", 9503);
// 可传递参数,这里获取缓存好的access_token
$sendData = [
'access_token' => curl_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的app_id&secret=你的app_secret')['access_token']
];
if (empty($ret)) {
shell_exec("echo '连接swoole服务器失败'>>");
} else {
$res = $client->send(json_encode($sendData));
var_dump($ret);
}
}
function curl_get($url)
{
// 初始化
$curl = curl_init();
// 设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
// 设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 0);
// 设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 执行命令
$data = curl_exec($curl);
// 关闭URL请求
curl_close($curl);
if (isset(json_decode($data, true)['errcode'])) {
return json_decode($data, true);
} else {
// 返回数据
return json_decode($data, true);
}
}
|