'wx89c12dd426a55a2e', 'appSecret' => '33e66bcf944f9810abbb5ddd7825403d', 'token' => 'cxhxy', ]; } public function index() { if (isset($_GET['echostr'])) { echo $_GET["echostr"]; exit; } //接收微信推送数据 $data = file_get_contents('php://input'); // $data = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA); if (!empty($data)) { // $data = json_decode(json_encode($data), true); $data = json_decode($data, true); $openid = $data['FromUserName']; if (!empty($data)) { //首次关注 if ($data['Event'] == 'subscribe') { $user = Db::name('food_user')->where('open_id', $openid)->value('is_new'); if (!empty($user) && $user !== 1) { //不是新关注 die('success'); } else { if (empty($user)) { //插入一条新用户数据 $data = [ 'open_id' => $openid, 'platform' => 10, 'applet_id' => 10001, 'create_time' => time() ]; Db::name('food_user')->insert($data); } //发送公众号消息 $wx = new Driver; $access_token = $wx->getAccessToken(10001, 2); $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token; $queryarr = [ 'touser' => $openid, 'msgtype' => 'news', 'news' => [ 'articles' => [ 'title' => '您有一张新的优惠券,请及时领取', 'picurl' => '', 'url' => 'https://www.baidu.com', 'description' => '黄辛一新人优惠,首次关注赠送优惠券' ] ] ]; $result = Http::post($url, hema_json($queryarr)); write_log($result, __DIR__); } die('success'); } } } } }