阅读 58

PHP微信三方平台-代公众号发送消息模板

1、微信三方平台代公众号实现业务接口API文档地址:

2、微信三方平台代公众号发送消息模板的模板ID是不一样的所以在公众号添加模板需要将模板ID告知三方平台开发保存数据库

3、代码实现

php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/8
 * Time: 14:48
 */

namespace app\models\WeChatSendMessage;


use app\librarys\wx\wx_common\wx_tools;
use app\librarys\wx\wx_customer\wx_customer;

class WxPushMsg
{
    /**
     * pushMessage 发送自定义的模板消息
     * @param array $data 模板数据
     * $data = [
     * ‘openid‘ => ‘‘, 用户openid
     * ‘url‘ => ‘‘, 跳转链接
     * ‘template_id‘ => ‘‘, 模板id
     * ‘data‘ => [ // 消息模板数据
     * ‘first‘    => [‘value‘ => urlencode(‘黄旭辉‘),‘color‘ => "#743A3A"],
     * ‘keyword1‘ => [‘value‘ => urlencode(‘男‘),‘color‘=>‘blue‘],
     * ‘keyword2‘ => [‘value‘ => urlencode(‘1993-10-23‘),‘color‘ => ‘blue‘],
     * ‘remark‘   => [‘value‘ => urlencode(‘我的模板‘),‘color‘ => ‘#743A3A‘]
     * ]
     * ];
     * @param string $topcolor 模板内容字体颜色,不填默认为黑色
     * @return array
     */


    /**
     *
     *
     * @param $wx_openid
     * @param array $data
     * @param $template_id
     * @param string $league_id
     * @return array|mixed
     */
    public static function pushMessage($wx_openid, $data = [], $template_id, $league_id = ‘‘)
    {
        $template = [
            ‘touser‘ => $wx_openid,
            ‘template_id‘ => $template_id,
            ‘url‘ => isset($data[‘url‘]) ? $data[‘url‘] : "",
            ‘topcolor‘ => ‘#0000‘,
            ‘data‘ => $data[‘data‘]
        ];
        $json_template = json_encode($template);
        $app_id = ‘‘;//注意这是微信公众号的app_id
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . wx_customer::get_access_token($app_id);
        $result = wx_tools::postCurl($url, urldecode($json_template));
        $resultData = json_decode($result, true);
        return $resultData;
    }


    /**
     * 门店下单成功推送
     * @param $order_code
     * @param $customer_id
     * @return array
     */
    public static function leagueCreateOrderPush()
    {
        //获取用户默认登录信息
        $data = array();
        $data[‘url‘] = ‘http://weixin.qq.com/download‘;
        $data[‘data‘] = array( // 模板消息内容,根据模板详情进行设置
            ‘first‘ => [‘value‘ => urlencode("您好,您的订单提交成功。"), ‘color‘ => "#743A3A"],
            ‘keyword1‘ => [‘value‘ => urlencode(‘我是订单号测试0001‘)],
            ‘keyword2‘ => [‘value‘ => urlencode(date(‘Y年m月d日 H:i:s‘, time()))],
            ‘keyword3‘ => [‘value‘ => urlencode("门店订单")],
            ‘keyword4‘ => [‘value‘ => urlencode(‘‘)],
            ‘remark‘ => [‘value‘ => urlencode("点击查看详细信息。")]
        );
        $re = self::pushMessage($open_id, $data, $tempt_id, ‘‘);
        return $re;
    }
}

 

原文:https://www.cnblogs.com/lt-com/p/12910695.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐