首先把下面的WxTemple放在\www\ThinkPHP\Library\Vendor下
class sendMessage {
//获取accesstoken
function getAccesstoken(){
$ch=curl_init();
$appid="";//公众号的appid;
$appsecret="";//公众号的appsecret;
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
//GET方式抓取URL
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//执行
$outopt=curl_exec($ch);
$outoptarr=json_decode($outopt,TRUE);
return $outoptarr['access_token'];
}
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;
}
}
然后在控制器里写个test方法测试下
//消息通知函数
public function test(){
Vendor('Weixinpay.WxTemple');
$sendMes = new \sendMessage();
//data的数据根据模板的不同内容不同
$template=array(
'touser'=>'',//接收模板消息的用户openid
'template_id'=>"",//模板消息的id
'url'=>'http://www.baidexuan.com',
'topcolor'=>"#7B68EE",
'data'=>array(
'first'=>array('value'=>urlencode("蜗居配送通知"),'color'=>"#743A3A"),
'Day'=>array('value'=>urlencode(date('Y-m-d H:i:s',time())),'color'=>'#333'),
'orderId'=>array('value'=>urlencode(time()),'color'=>'#333'),
'orderType'=>array('value'=>urlencode("蜗居配送"),'color'=>'#333'),
'customerName'=>array('value'=>urlencode("小蜗"),'color'=>'#333'),
'customerPhone'=>array('value'=>urlencode("17777777777"),'color'=>'#333'),
'remark'=>array('value'=>urlencode('请及时去处理订单,'),'color'=>'#DD5044'),
)
);
$access_token = $sendMes->getAccesstoken();
$json_template=json_encode($template);
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$res=$sendMes->http_request($url,urldecode($json_template));
if ($res[errcode]==0){
echo 1;
}else{
echo -1;
};
}
阿里云学生机1年114元限时活动(24岁以下都可以购买)https://promotion.aliyun.com/ntms/act/campus2018.html?userCode=a6violqw阿里云1888元红包:https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=a6violqw