写的是自己看得懂的记录。未经过整理
//频道动态详情
$info = db('dynamic')->field('id,user_id,title,point,form_id,use_form_id')->where('id',$dynamic_id)->find();
//推送
if(!empty($info['form_id']) && $info['use_form_id'] != 1){
$user_data = db('user')->field('xcx_openid,user_realname')->where(['id'=>$info['user_id']])->find();
$toUser = $user_data['xcx_openid'];
$user_realname = $user_data['user_realname'];//点赞人
$template_id = 'N4zvdShk*************************Ka_Q_zaj7owqPLI';
$json = '{
"keyword1": {
"value": "'.$info['title'].'",
"color": "#173177",
"size": "10"
},
"keyword2": {
"value": "'.$user_realname.'",
"color": "#173177"
},
"keyword3": {
"value": "'.date('Y-m-d H:i:s',time()).'",
"color": "#173177"
}
}';
$this->tuisong($toUser,$info['form_id'],$json,$template_id,$dynamic_id);
}
public function tuisong($toUser,$form_id,$json,$template_id,$dynamic_id){
// 微信设置
$weixinInfos = cmf_get_weixin_infos();
$appId =isset($weixinInfos['xcx_appid'])?$weixinInfos['xcx_appid']:'';
$appsecret = isset($weixinInfos['xcx_appsecret'])?$weixinInfos['xcx_appsecret']:'';
$access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appsecret";
$result = $this->_get($access_token_url);
$token_info = json_decode($result, true);
$accesstoken = $token_info['access_token'];
$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$accesstoken;
if(!empty($toUser) || !empty($form_id)){
$j= '{
"touser": "'.$toUser.'",
"template_id": "'.$template_id.'",
"page": "",
"form_id": "'.$form_id.'",
"data": '.$json.'
}';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $j,
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
db('dynamic')->where(['id'=>$dynamic_id])->update(['use_form_id' => 1]);//更新推送id为已使用
if ($err) {
error_log('点赞失败:'.print_r($err,1)."\r\n",3,'dynamic_like.log');
} else {
error_log('点赞成功:'.print_r($response,1)."\r\n",3,'dynamic_like.log');
}
}
}
/**
* 模拟GET请求
*/
public function _get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //请求URL
curl_setopt($ch, CURLOPT_HEADER, 0); //过滤HTTP头
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//SSL证书认证
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$rs = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
return false;
} else {
curl_close($ch);
return $rs;
}
}