模板信息推送:需要在后台开通了模板消息推送接口
PHP代码如下:
<?php  
$appid="wxxxxxxxxxxxxxxxxxx";       //填写微信后台的appid 
$appsecret="xxxxxxxxxxxxxxxxxxxxxxxxx";   //填写微信后台的appsecret 
$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,true); 
$ACCESS_TOKEN=$result['access_token'];  #获取ACCESS_TOKEN,在expire_time字段可以看到过期时间,7200=两个小时,以秒为单位
$times=date("Y-m-d H:i:s",time()); 
$name='test';//标题
$zu='test1';//正文
//测试机账号
$openid='oxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$template=array( 
'touser'=>$openid, 
'template_id'=>"xxxxxxxxxxxxxxxxxxxxxxxxxxx",    //模板的id 
'url'=>"http://weixin.qq.com/download", 
'topcolor'=>"#FF0000", 
'data'=>array( 
'test'=>array('value'=>urlencode($name),'color'=>"#00008B"),    //函数传参过来的name      
'test1'=>array('value'=>urlencode($zu),'color'=>'#00008B'),        //函数传参过来的zu 
) 
); 
$json_template=json_encode($template); 
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN; 
$ch = curl_init(); 
$data=urldecode($json_template);
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); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
$output = curl_exec($ch); 
curl_close($ch); 
$arr = json_decode($output, true);
echo $arr['errcode'];
?>访问php文件网址,则得到:
任何程序错误,以及技术疑问或需要解答的,请扫码添加作者VX