折雨的天空

新浪微薄腾讯微薄

最新碎语:最近霉到了住。哎。。。

您的位置:折雨的天空 >php开发> easywechat6推送动态模板消息

easywechat6推送动态模板消息

需求,从接口获取模板列表,并对获取到模板进行赋值

尝试多次,主要在构造data数组的地方:

$app=util::getWeiXin();
        $api = $app->getClient();
        $response = $api->post('/cgi-bin/template/get_all_private_template');
        $tempateList=$response->toArray();

        //尝试发送一条模板消息 
        //随机一个模板消息
        $randNum=rand(0,count($tempateList['template_list'])-1);
        $template=$tempateList['template_list'][$randNum];
        //匹配变量
        preg_match_all('~{{(.*)}}~',$template['content'],$match);
        $keyword=[];
        foreach ($match[1] as $k=>$v){
            $key=substr($v,0,strpos($v,'.'));
            $keyword[$key]['value']='测试新'.($k+1);
            if (strpos($key,'time')!==false){
                $keyword[$key]['value']=date('Y-m-d H:i:s');
            }
            if (strpos($key,'amount')!==false){
                $keyword[$key]['value']=rand(1000,2000);
            }
            if (strpos($key,'character')!==false){
                $keyword[$key]['value']=uniqid();
            }
        }
        //发送模板消息
        $response = $api->post('/cgi-bin/message/template/send', [
            'json' => [
                "touser" => "openid",
                "template_id" => $template['template_id'],
                "url" => "http://www.baidu.com",
                "data"=> $keyword
            ]
        ]);
        var_dump($response->getContent());

参考:https://blog.csdn.net/quweiie/article/details/127939474

------------正 文 已 结 束, 感 谢 您 的 阅 读 (折雨的天空)--------------------

转载请注明本文标题和链接:《easywechat6推送动态模板消息

奖励一下

取消

分享不易,烦请有多多打赏,如您也困难,点击右边关闭即可!

扫码支持
扫码打赏,5元,10元,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

-秒后自动关闭,如已打赏,或者不愿打赏,请点击右上角关闭图标。

发表评论

路人甲 表情
看不清楚?点图切换