主页

PHP 微信公众号发送模板消息

姬千年   IP属地: 青海
0 点赞 0阅读 1907

上代码:

<?php
include_once '../common/system.class.inc.php';
$user_id = $row_user_v['id'];
$id = $_GET['id']?$_GET['id']:0;

//具体业务逻辑根据自己业务定,我这里是给所有订阅消息的用户发送消息
$custom_sql="select  count(DISTINCT c.user_id) as count, c.id,u.wxopenid,c.keyword,c.is_notify,c.type,l.typename as city ,m.typename as `column`,c.city as city_id,c.`column` as column_id   from tb_custom as c left join tb_lmtype AS l on c.city=l.id left join tb_lmtype AS m on c.column=m.id right join tb_user AS u on c.user_id=u.id AND u.wxopenid !='' where  is_notify=1 group by c.user_id ORDER BY `updatetime` DESC  ";
	$custom_rs=$crud->ExecSQL($custom_sql,$conn);
while ($rows = mysql_fetch_array($custom_rs, MYSQL_ASSOC)) {
	$count = count($rows); //不能在循环语句中,由于每次删除 row数组长度都减小
		//template();
		$wxopenid = $rows['wxopenid'];
		$keyword = $rows['keyword'];
		$column= $rows['column']?$rows['column']:'全部栏目';
		template($wxopenid,$column,$keyword);//发送模板消息
	
}

//template('o-mmd6iCSs80hGub09Kac3DOBmuA','全部栏目','法律');//发送模板消息,上面的逻辑代码可以忽略

//------------------------------------获取access_token的方法-----------------------------------//。
	 function getToken()
	{
		
		$appid = 'wx474d0c9b54aab6a0';//公众号appid
	   $appsecret = 'eb844c522a7d7054db97d1a4ffb392ce';//公众号appsecret
	 $token_file = dirname(dirname(__FILE__)).'/mobile/token.txt';
	  
	 if(!file_exists($token_file) || ((time() - filemtime($token_file)) > 7000)){
	  $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
	  $json=file_get_contents($TOKEN_URL);
	  $result=json_decode($json);
	  $ACC_TOKEN=$result->access_token;
	  file_put_contents($token_file,$ACC_TOKEN);
	 }else{
	  $ACC_TOKEN = file_get_contents($token_file);
	 }
	 
	 return $ACC_TOKEN;
	}
	//--------------------------模板消息发送------------------------//
	function template($openid,$keyword1,$keyword2){
		$ACCESS_TOKEN = getToken();//通过微信获取access_token接口 获取的token
		$openid = $openid;//'o-mmd6ojaoNYKWA8PzOJXSHvdWfc';//用户openid
		$template_id = 'dFMSIXtmlfqtSi5L9N1W8qfjKlNcSIXcN_0AqA-otvQ';//配置的模板id
		$url = 'http://www.china-bxzb.com/mobile';//点击模板消息跳转的链接

		$template = array(
			'touser' => $openid,
			'template_id' => $template_id,
			'url' => $url,
			'data' => array(
				'first' => array('value' => '您订阅的项目有更新,请您确认相关项目信息。!', 'color' => "#173177"),
				'keyword1' => array('value' => $keyword1, 'color' => '#173177'),
				'keyword2' => array('value' => $keyword2, 'color' => '#173177'),
				'remark' => array('value' => '更多招标项目及分类细项等信息请点击进入查看。!', 'color' => '#173177'),)
		);

		$send_template_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $ACCESS_TOKEN;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $send_template_url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($template));
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$res = curl_exec($ch);
		curl_close($ch);
		return $res;

	}

?>

效果如图:







© 著作权归作者所有,转载或内容合作请联系作者