微信模版消息+ThinkPHP


首先我自己创建了个Model :

<?php
namespace Qwadmin\Model;
//  微信消息处理类
class WxmsgModel {
	protected $appid;
	protected $secrect;
	protected $accessToken;
	function  __construct()
	{
		$this->appid = C('APPID');
		$this->secrect = C('APPSECRET');
		$this->accessToken = $this->getToken($this->appid, $this->secrect);
	}
	/**
	 * 发送post请求
	 * @param string $url
	 * @param string $param
	 * @return bool|mixed
	 */
	function request_post($url = '', $param = '')
	{
		if (empty($url) || empty($param)) {
			return false;
		}
		$postUrl = $url;
		$curlPost = $param;
		$ch = curl_init(); //初始化curl
		curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
		curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
		curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
		$data = curl_exec($ch); //运行curl
		curl_close($ch);
		return $data;
	}
	/**
	 * 发送get请求
	 * @param string $url
	 * @return bool|mixed
	 */
	function request_get($url = '')
	{
		if (empty($url)) {
			return false;
		}
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 500);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
	/**
	 * @param $appid
	 * @param $appsecret
	 * @return mixed
	 * 获取token
	 */
	protected function getToken($appid, $appsecret)
	{
		if (S($appid)) {
			$access_token = S($appid);
		} else {
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
			$token = $this->request_get($url);
			$token = json_decode(stripslashes($token));
			$arr = json_decode(json_encode($token), true);
			$access_token = $arr['access_token'];
			S($appid, $access_token, 720);
		}
		return $access_token;
	}
	/**
	 * 发送自定义的模板消息
	 * @param $touser
	 * @param $template_id
	 * @param $url
	 * @param $data
	 * @param string $topcolor
	 * @return bool
	 */
	public function doSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE')
	{
		$template = array(
			'touser' => $touser,
			'template_id' => $template_id,
			'url' => $url,
			'topcolor' => $topcolor,
			'data' => $data
		);
		$json_template = json_encode($template);
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken;
		$dataRes = $this->request_post($url, urldecode($json_template));
		if ($dataRes['errcode'] == 0) {
			return true;
		} else {
			return false;
		}
	}
}
在控制器中调用:

 function test(){
        $wxmsg = D('Wxmsg');
        $data=array(
            'first'=>array('value'=>urlencode("您好,你的投诉正在受理中"),'color'=>"#00CD00"),
            'keyword1'=>array('value'=>urlencode("豫J548726"),'color'=>'#EE5C42'),
            'keyword2'=>array('value'=>urlencode("2016-12-15 12:53"),'color'=>'#030303'),
            'keyword3'=>array('value'=>urlencode("区县公安局正在受理"),'color'=>'#EE9A00'),
            'keyword4'=>array('value'=>urlencode("13717674036"),'color'=>'#030303'),
            'remark'=>array('value'=>urlencode('感谢您的反馈'),'color'=>'#030303'),
        );
        $wxmsgs = $wxmsg->doSend('oUC0WwPS-J79WJ1Wciv3eOhxjmLo','L8Z3bP5IfdG2Naxau2OkktdugcaO4TxKay9dyeCUAok','http://www.cqhbwqts12369.com/index.php/Home/List/index.html',$data);
        echo '121';die;
    }



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值