public function template($id)
{
$user = User::where('id',$id)->value('openid');//微信openid
$url = $_SERVER['HTTP_HOST'];
$wxurl = 'https://api.weixin.qq.com/cgi-bin/token/template/send?access_token=ACCESS_TOKEN';
$weixin = Weixin::where('id',1)->select();
$appid = $weixin[0]['appid'];
$secret = $weixin[0]['appsecret'];
//获得access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$weix = file_get_contents($url);//获得网页输出
$obj=json_decode($weix,true );//解码
$access_token= $obj['access_token'];//网页授权接口调用凭证
//发送模板消息
$fasuerl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$data = array(
"touser"=>$user,
"template_id"=>"2GSNHCC4xOrz9p9fqmKLbUwknwVa1iZnClqzbQb5xhw",
"data" => array(
"first" => array(
"value"=>"咨询通知",
"color"=>"#173177"
),
"keyword1" => array(
"value"=>"巧克力!",
"color"=>"#000000"
),
"keyword2" => array(
"value"=>"巧克力!",
"color"=>"#000000"
),
"remark" => array(
"value"=>"巧克力!",
"color"=>"#000000"
),
)
);
$params = json_encode($data);
$res=$this->curl_post($fasuerl,$params);
print_r($res);
}
//发送post请求
function curl_post($url , $data=array()){
$ch = curl_init();//创建curl请求
curl_setopt($ch, CURLOPT_URL,$url); //设置发送数据的网址
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设置有返回值,0,直接显示
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用证书验证
curl_setopt($ch, CURLOPT_POST, 1);//post方法请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post请求发送的数据包
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data,true); //将json数据转成数组
return $data;
}