image.png

Subscription accounts can only be sent in groups once a day, and service accounts can only be sent in groups four times a month; how can I actively send more information to fans? The problem of increasing the number of group postings has always plagued many WeChat operators. The Little Green Elephant Software Studio has developed a function of 48 hours unlimited push of information using PHP. The following is a demonstration with PHP code example, which can be adjusted to other The programming language logic is the same

Obtain AccessToken

public function getAccessToken()
{
    $result = $this->redis->get($this->accessTokenCacheKey);
    if($result) {
        return $result;
    }

    $ret = $this->httpClient->request('GET','/cgi-bin/token',[
        'query' => [
            'grant_type' =>'client_credential',
            'appid' => $this->appid,
            'secret' => $this->appSecret
        ]
    ]);

    $ret = json_decode($ret->getBody()->getContents(),true);
    if($ret && isset($ret['access_token']) && !empty($ret['access_token'])) {
        $expire = $ret['expires_in']-200;
        $this->redis->setex($this->accessTokenCacheKey,$expire,$ret['access_token']);
        return $ret['access_token'];
    }

    return false;

}

Get a list of fans

public function getOpenIdList()
{
    $this->accessToken = $this->getAccessToken();
    $ret = $this->httpClient->request('GET','/cgi-bin/user/get',[
        'query' => [
            'access_token' => $this->accessToken
        ]
    ]);
    $ret = json_decode($ret->getBody()->getContents(),true);
    if($ret && !isset($ret['errcode'])) {
        return $ret['data']['openid'];
    }
}

Push messages to all fans

public function sendMessageByOpenIdList(array $openIdList = [])
{
    $success = 0;

    $this->accessToken = $this->getAccessToken();

    $msg = "Little Lord, it's time to eat!\n". date('m月d日'). "Today's red envelope has been updated:\n————————————\n\n 🔜 <a href=\"weixin://bizmsgmenu?msgmenucontent=[click me] hungry red envelope&msgmenuid=0\">[click me] hungry red envelope</a>\n\n🔜 <a href= \"weixin://bizmsgmenu?msgmenucontent=[Click Me] Meituan Takeaway Red Packet&msgmenuid=0\">[Click Me] Meituan Takeaway Red Packet</a>\n\n—————————— ——\N😋Put your official account on top and receive red envelopes for takeaway every day";

    if($openIdList) {

        foreach($openIdList as $openId) {

            $data = [
                'touser' => $openId,
                'msgtype' =>'text',
                'text' => [
                    'content' => $msg
                ]
            ];

            $data = json_encode($data,JSON_UNESCAPED_UNICODE);

            $ret = $this->httpClient->request('POST','/cgi-bin/message/custom/send',[
                'query' => [
                    'access_token' => $this->accessToken
                ],
                'body' => $data
            ]);

            $ret = json_decode($ret->getBody()->getContents(),true);

            if($ret['errcode'] == 0) {
                $success += 1;
            }

        }

    }

    return $success;
}

Add timer

0 11,17 * * * php /www/wwwroot/demo/think WeChatPushMsg >> /www/wwwroot/demo/runtime/log/wechat_push.log
点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部