thinkphp发模板消息/**
* @description: 发循环
* @param {*}
* @return {*}
*/
public function sendall(){
$all_openid=$this->tosendall();
foreach($all_openid as$value){
$res= Db::name('*****')->where('open_id',$value)->find();
if(!empty($res)){
$this->set_msg($res);
}
}
exit;
}
/**
* @description: curl
* @param {*}
* @return {*}
*/
public function tosendall(){
$access_token=getAccesstoken();
$url="api.weixin.qq/cgi-bin/user/get?access_token={$access_token}&next_openid=";
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
$data=curl_exec($ch);
$data=json_decode($data,true);
return$data['data']['openid'];
}
/**
* @description: 发送消息
* @param {*} $openid
* @return {*}
*/
public function set_msg($values){
$access_token=getAccesstoken();
$openid=$values['open_id'];
$username=$values['username'];
无法访问移动网络$datetime=date("Y-m-d",time());
$template=array(
"touser"=>"$openid",//⽤户openid
"template_id"=>"*******",//你的模板id
"url"=>"",//跳转链接
"data"=>array(
'first'=>array('value'=>urlencode("今天您打卡了么?")),
'keyword1'=>array('value'=>urlencode("$username")),
'keyword2'=>array('value'=>urlencode("$datetime")),
'keyword3'=>array('value'=>urlencode("您还未打卡")),
)
);
$json_template=json_encode($template);
$url="api.weixin.qq/cgi-bin/message/template/send?access_token=".$access_token;
$json=$this->curl_post($url,urldecode($json_template));
$rs=json_decode($json,true);
// if ($rs['errcode'] == 0) {
// echo 'ok';
// } else {
// echo "no";
己组词// print_r($rs);
// }
山东好玩的地方}
function curl_post($url,$data){
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
if(!empty($data)){
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$output=curl_exec($curl);
curl_close($curl);
return$output;
}
公共函数库
function getAccessToken(){
// access_token 应该全局存储与更新,以下代码以写⼊到⽂件中做⽰例
// 如果是企业号⽤以下URL获取access_token
// $url = "qyapi.weixin.qq/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$cacheKey='cache:access:token';
if(Cache::has($cacheKey)){
return Cache::get($cacheKey);
}else{
$url="api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid=".config('user.appid')."&secret=".config('user.secret');
$res=json_decode(httpGet($url));
$access_token=$res->access_token;
Cache::set($cacheKey,$access_token,60*5);
毛利计算公式return$access_token;外贸实习日记
}
}
function httpGet($url,$data=null,$method='get',$header=array("content-type: application/json"),$https=true,$timeout=5){
$method=strtoupper($method);
$ch=curl_init();//初始化
助力泵curl_setopt($ch,CURLOPT_URL,$url);//访问的URL
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//只获取页⾯内容,但不输出
if($https){
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//https请求不验证证书
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);//https请求不验证HOST
}
if($method!="GET"){
if($method=='POST'){
curl_setopt($ch,CURLOPT_POST,true);//请求⽅式为post请求
}
if($method=='PUT'||strtoupper($method)=='DELETE'){
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$method);//设置请求⽅式
}
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//请求数据
}
curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);//模拟的header头
//curl_setopt($ch, CURLOPT_HEADER, false);//设置不需要头信息
$result=curl_exec($ch);//执⾏请求
curl_close($ch);//关闭curl,释放资源
return$result;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论