PHP Laravel框架 微信模板消息发送

laravel框架 微信模板消息发送

    /**
     * 发送模板消息
     */
    public function add(Request $request,$id){
        $house_data = House::find($id);
        $phone = $request->input('phone','');
        $template=array(
            'touser'=>'', //接收消息模版用户的openid
            'template_id'=>"", //在公众号下配置的模板id
            'url'=>"", //点击模板消息会跳转的链接
            'topcolor'=>"#7B68EE",
            'data'=>array(
                 //'first'=>array('value'=>urlencode("微信模版消息发送测试"),'color'=>"#FF0000"),
                'keyword1'=>array('value'=>$house_data->title,'color'=>'#FF0000'), //keyword需要与配置的模板消息对应
                'keyword2'=>array('value'=>$request->btn,'color'=>'#FF0000'),
                'keyword3'=>array('value'=>$house_data->site,'color'=>'#FF0000')
            )
        );
        
                    $token = session("access_token_mp");
            if($token){
                return $token;
            }
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的aphid&secret=公众号的secret";

            $res = $this->curl_post($url);
            $td = json_decode($res, true);

            if($td['access_token']){
                $access_token = session("access_token_mp", $td['access_token'], $td['expires_in'] - 120);//获取微信公众平台token

                session(['weixin_token'=>$access_token]);
                
                $json_template=json_encode($template);
                $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
                $res=$this->curl_post($url,urldecode($json_template));
                //   dd($res);
                echo '<script>alert("微信模版消息发送成功");location="'.$_SERVER['HTTP_REFERER'].'"</script>';
//                    return $td['access_token'];
            }else{
                return false;
            }
            
            exit;


    }

下面展示一些 内联代码片

    /**
     * @param $url
     * @param array $data
     * @return mixed
     * curl请求
     */
    function curl_post($url , $data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值