微信模板消息---php

微信模板消息备注

<?php

/**
 * Created by PhpStorm.
 * Time: 上午 9:46
 */
class topapi_sendNewOrderMsg
{
        private $appid = '*****';
        private $secret = '*******';

    //微信模板消息
    function send($params)
    {
        //获取token
        $access = json_decode($this->get_access_token(), true);
        $access_token = $access['access_token'];
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
        $openId=$this->getShopOpenId($params['shop_id']);
        //组装数组
        $array = array(
            'touser' => $openId,
            'template_id' => 'tU7LpzrkXqmJO_QRii45kduBYIhUXB0iaXaNU2cjdLc',
            'url' => $params['page'],
            'data' => array(
                'first' => array('value' => "订单生成通知", 'color' => '#173177'),
                'keyword1' => array('value' => date('Y-m-m H:i:s'), 'color' => '#173177'),
                'keyword2' => array('value' => $params['name'], 'color' => '#173177'),
                'keyword3' => array('value' => $params['order_sn'], 'color' => '#173177'),
                'remark' => array('value' => '订单成功。', 'color' => '#173177'),
            )
        );
        //数组->json
        $postJson = json_encode($array);
        //调用函数
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postJson);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    //获取access_token
    public function get_access_token()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->secret}";
        return $data = $this->curl_get($url);
    }

    public function curl_get($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        return $data;
    }

    //获取商家openid
    public function getShopOpenId($shop_id)
    {
        //通过shop_id获取seller_id
        $seller_id = app::get("sysshop")->model("seller")->getRow('seller_id', array('shop_id' => $shop_id, 'seller_type' => 0))['seller_id'];
        //通过seller_id获取user_id
        $user_id = app::get("sysshop")->model("account")->getRow('user_id', array('seller_id' => $seller_id))['user_id'];
        //通过user_id获取openid
        $openId = app::get("sysuser")->model("account")->getRow('openid', array('user_id' => $user_id))['openid'];
        return $openId;
    }

    //获取客户openid
    public function getCustomerOpenId($order_sn)
    {
        $user_id = app::get("systrade")->model("trade")->getRow('user_id', array('tid' => $order_sn))['user_id'];
        $unionid = app::get("sysuser")->model("account")->getRow('unionid', array('user_id' => $user_id))['unionid'];
        $openId = app::get("sysuser")->model("customer_msg")->getRow('openid', array('unionid' => $unionid))['openid'];
        return $openId;
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要发送微信模板消息,需要先在微信公众平台或开放平台申请相关的权限和模板消息ID。然后,可以使用PHP的curl库或者第三方SDK来发送模板消息。 以下是一个使用curl库发送微信模板消息的示例代码: ```php <?php // 模板消息的接口地址 $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN"; // 模板消息的内容,需要根据实际情况替换 $data = array( 'touser' => 'OPENID', 'template_id' => 'TEMPLATE_ID', 'url' => 'http://www.example.com', 'data' => array( 'first' => array('value' => '您好,您有一条新的订单'), 'keyword1' => array('value' => '订单号:123456'), 'keyword2' => array('value' => '商品名称:测试商品'), 'keyword3' => array('value' => '订单金额:100元'), 'remark' => array('value' => '感谢您的支持!'), ), ); // 将数组转换为JSON字符串 $json = json_encode($data); // 使用curl库发送POST请求 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); // 输出发送结果 echo $result; ?> ``` 其中,`ACCESS_TOKEN`是调用微信接口所需的access_token,需要先通过接口获取;`OPENID`是接收模板消息的用户的openid;`TEMPLATE_ID`是申请的模板消息ID;`data`是模板消息的内容,需要根据实际情况替换。 注意:在实际使用时,需要根据微信接口的要求进行签名验证等安全处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值