开发环境:PhpStorm+Xampp(pthread多线程插件) 第二,后台方面,涉及的批量推送的话,就需要用到php多线程扩展了,这个项目中用的则是pthread,下载地址与安装方法在文章最前面。 多线程安装成功的标志 微信小程序发送服务通知具体细节请看https://developers.weixin.qq.com/miniprogram/dev/api/notice.html#%E6%A8%A1%E7%89%88%E6%B6%88%E6%81%AF%E7%AE%A1%E7%90%86
pthread插件下载地址:http://windows.php.net/downloads/pecl/releases/pthreads/
pthread扩展安装方法:重点来了
在后台代码中继承Thread类,重写它的run()方法,并在控制器中调用成功。如下:<?php/**
* Created by PhpStorm.
* User: zw
* Date: 2018/8/16
* Time: 10:59
*/namespace app\api\service;use Thread;class WxPushService extends Thread{ protected $openid = "";//微信用户openid
protected $formid = "";//对应表单id
protected $name = "";//服务通知字段
protected $columnid = 0; public function __construct($openid, $formid, $name, $columnid)
{ $this->openid = $openid; $this->formid = $formid; $this->name = $name; $this->columnid=$columnid;
} public function run()
{
$data = array( 'touser' => $this->openid, 'template_id' => 'xxxxxxxxxxxxxxx', 'page' => 'xxxxxxxxxxxxxxxx', "form_id" => $this->formid, 'topcolor' => '#FF0000', 'data' => array( 'keyword1' => array( 'value' => $this->name
), 'keyword2' => array( 'value' => $this->name
), 'keyword3' => array( 'value' => $this->name
)
)
);//服务通知发送必要字段
$params = json_encode($data, JSON_UNESCAPED_UNICODE);
$result = curl_post_raw("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" . $accessToken, $params);//其中$accessToken为发送服务通知的安全令牌
}
}
最后在控制器中通过遍历之前存的formid通过for循环执行多个子线程。
作者:极客简讯
链接:https://www.jianshu.com/p/b2f1fca228c5
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章