laravel框架 微信模板消息发送
public function add(Request $request,$id){
$house_data = House::find($id);
$phone = $request->input('phone','');
if (empty($phone)) {
echo '<script>alert("请填写手机号!");location="'.$_SERVER['HTTP_REFERER'].'"</script>';
exit;
}
if(!preg_match('/^1[0-9]{10}$/', $phone)){
echo '<script>alert("请填写正确的手机号!");location="'.$_SERVER['HTTP_REFERER'].'"</script>';exit;
}
$tab = new Sem;
if (empty($request->name)) {
$tab->name = '用户选择姓名保密';
}else{
$tab->name = $request->input('name','');
}
$tab->phone = $request->input('phone','');
$tab->hid = $request->input('hid',$id);
$tab->ditch = $request->input('btn','');
$tab->antistop = $request->input('antistop','关键词');
$tab->created_at = date('Y-m-d H:i:s', time());
$tab->updated_at = date('Y-m-d H:i:s', time());
$tab->save();
$template=array(
'touser'=>'',
'template_id'=>"",
'url'=>"",
'topcolor'=>"#7B68EE",
'data'=>array(
'keyword1'=>array('value'=>$house_data->title,'color'=>'#FF0000'),
'keyword2'=>array('value'=>$request->btn,'color'=>'#FF0000'),
'keyword3'=>array('value'=>$house_data->site,'color'=>'#FF0000'),
'keyword4'=>array('value'=>urlencode(date("Y-m-d H:i:s")),'color'=>'#FF0000')
)
);
$token = session("access_token_mp");
if($token){
return $token;
}
$url = "";
$res = $this->curl_post($url);
$td = json_decode($res, true);
if($td['access_token']){
$access_token = session("access_token_mp", $td['access_token'], $td['expires_in'] - 120);
session(['weixin_token'=>$access_token]);
$json_template=json_encode($template);
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$res=$this->curl_post($url,urldecode($json_template));
echo '<script>alert("售楼经理将会1小时内联系您");location="'.$_SERVER['HTTP_REFERER'].'"</script>';
}else{
return false;
}
exit;
}
下面展示一些 内联代码片 。
function curl_post($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);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
|