欢迎访问WDPHP素材源码!今天是2024年04月24日 星期三,天亮要了!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 源码 > 微信/小程序 > 

微信模板消息发送样例
栏目分类:微信/小程序    发布日期:2017-07-31    浏览次数:4441次     收藏

微信模板消息发送样例:
//先构建一个http发送
    function http_request($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;
    }

	//然后可以去实现发送,中间获取openid的就不写了 我这里直接给大家展示可以用的,你去修改一些微信公众号的数据就行
	//去实现模板发送吧

 public function send(){
        echo "测试是否进入方法";
        $tpl = array(
            //接收者 这里面要用到用户的openid
            "touser"=>"",
            //这里是你要发送的模板类型
            "template_id"=>"LikmE1ecqhNMRnqFOBiJdQ9N0FT6ONA_lRiIlRgnZxw",
            //这里是点击模板跳转的页面
            "url" => "www.wdphp.com",
            //标题颜色
            "topcolor" => "#FFAE89",
            "data" => array(
			//这里的first,keyword 都要对应这模板里面的信息写
                "first" => array("value" => "学生到校通知","color" => "#FF83FA"),
                "keyword1" => array("value" => "名字:小陈","color" => "#FF7536"),
                "keyword2" => array("value" => "班级信息","color" => "#FF69FA"),
                "keyword3" => array("value" => "时间".date('Y-m-d H:i:s'),"color" => "#FF7526"),

				//这里是通用的 就类似与备注的存在
                "remark" => array("value" => "谢谢您关注我们的公众号","color" => "#FF7256"),
            ),

        );

        $json_template = json_encode($tpl);
		//这里后面的sccse_token 要替换成自己的accse_token  
		//获取地址 点击这个  直接复制出去替换成自己的  会返回你的sccse_token的
		//https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET

        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ovmANbdqnWh--fZprl4nrN_d1TCqbo_x_hVzZTSuMPcASg-3sI-p7pZwGKTngytBHNlklBaZ9f5iEfJlXEWDH-IjtcmjkwMkhLN4crPFkEoONEdAFACBS";
        $res = json_decode($this->http_request($url,$json_template),true);
        if ($res["errcode"] == 0){
            echo "成了";
        }else{
            echo json_encode($res);
        }
    }

相关热词: 微信模板消息

源码 模板 特效 素材 资源 教程 站长