获取access_token和jsapi_ticket和过滤微信表情、发送模板消息

 

function get_access_token(){
$get_token=M('token')->where("name='access_token'")->find();
$access_token=$get_token['value'];
if(time() > $get_token['overtime']){
$config=C('WEIXINPAY_CONFIG');
$appid = $config['APPID'];
$secret = $config['APPSECRET'];
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$rurl = file_get_contents($url);
$rurl = json_decode($rurl,true);
if(array_key_exists('errcode',$rurl)){
return false;
}else{
$access_token = $rurl['access_token'];
$info['value']=$access_token;
$info['overtime']=time()+7000;
M('token')->where("name='access_token'")->save($info);
return $access_token;
}
}else{
return $access_token;
}
}
//获取jsapi_ticket
function get_jsapi_ticket(){
$jsapi_ticket=M('token')->where("name='jsapi_ticket'")->find();
$jsapi_ticket=$jsapi_ticket['value'];
if(time() > $jsapi_ticket['overtime']){
$access_token = get_access_token();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi";
$res = file_get_contents($url);
$res = json_decode($res,true);
if($res['errcode'] != '0'){
return false;
}else{
$jsapi_ticket = $res['ticket'];
$info['value']=$jsapi_ticket;
$info['overtime']=time()+7000;
M('token')->where("name='jsapi_ticket'")->save($info);
return $jsapi_ticket;
}
}else{
return $jsapi_ticket;
}
}
// 过滤掉emoji表情
function filter_Emoji($str)
{
$str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
'/./u',
function (array $match) {
return strlen($match[0]) >= 4 ? '' : $match[0];
},
$str);

return $str;
}

//发送模板消息

$data = array(
'first' => array('value' => urlencode("您在 {$ainfo['company_name']} 订购的商品有了新的处理进度"), 'color' => "#173177"),
'keyword1' => array('value' => urlencode($status), 'color' => "#173177"),
'keyword2' => array('value' => urlencode(date("Y-m-d H:i")), 'color' => '#000000'),
'keyword3' => array('value' => urlencode($money . '元'), 'color' => '#000000'),
'keyword4' => array('value' => urlencode('配货员'), 'color' => '#000000'),
'remark' => array('value' => urlencode(''), 'color' => '#000000'),
);

public function doSend($touser, $template_id, $data, $topcolor = '#7B68EE')
{
$template = array(
'touser' => $touser,
'template_id' => $template_id,
//'url' => '',
'topcolor' => $topcolor,
'data' => $data
);
$json_template = json_encode($template);
$access_token = get_access_token();
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
$dataRes = $this->request_post($url, urldecode($json_template));
return $dataRes;
}

/**
* 发送post请求
* @param string $url
* @param string $param
* @return bool|mixed
*/
public function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
//dump($postUrl);
//dump($curlPost);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
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, $curlPost);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}

posted @ 2018-01-15 10:56  渔夫三拳  阅读(168)  评论(0编辑  收藏  举报