对用户发送模板消息
微信实现给指定用户发送模板消息
原文链接:http://www.580bang.com/?post=8方法<br>
function http_request($url,$data=array()){<br>
$ch = curl_init();<br>
curl_setopt($ch, CURLOPT_URL, $url);<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);<br>
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);<br>
// POST数据<br>
curl_setopt($ch, CURLOPT_POST, 1);<br>
// 把post的变量加上<br>
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);<br>
$output = curl_exec($ch);<br>
curl_close($ch);<br>
return $output;<br>
}
代码$json_token=http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".C('WX_APPID')."&secret=".C('WX_SECRET'));<br>
$access_token=json_decode($json_token,true);<br>
//获得access_token<br>
$this->access_token=$access_token[access_token];<br>
//echo $this->access_token;exit;<br>
//模板消息<br>
$template=array(<br>
'touser'=>指定用户openid,<br>
'template_id'=>"模板代码",<br>
'url'=>"链接地址",<br>
'topcolor'=>"#7B68EE",<br>
'data'=>array(<br>
'first'=>array('value'=>urlencode($mcsinfo['mcs_name']."您好,您有新的订单"),'color'=>"#FF0000"),<br>
'keynote1'=>array('value'=>urlencode(date("Y-m-d H:i:s")),'color'=>'#FF0000'),<br>
'keynote2'=>array('value'=>urlencode('这是测试'),'color'=>'#FF0000'),<br>
'keynote3'=>array('value'=>urlencode('这是测试'),'color'=>'#FF0000'),<br>
'keynote4'=>array('value'=>urlencode('这是测试'),'color'=>'#FF0000'),<br>
'remark'=>array('value'=>urlencode('这是测试'),'color'=>'#FF0000'), )<br>
);<br>
$json_template=json_encode($template);<br>
//echo $json_template;<br>
//echo $this->access_token;<br>
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->access_token;<br>
$res=http_request($url,urldecode($json_template));<br>
if ($res[errcode]==0) echo '发送成功';
AD:真正免费,域名+虚机+企业邮箱=0元