- 检验当前浏览器是否是微信环境
- http获取微信token
- 发送微信模板信息
- 用户关注公众号
- 拉取公众号关注列表
注:具体接口情况以微信开发官方文档为准
/**
* 检验当前浏览器是否是微信环境
* @return bool 是微信里打开:true,不是微信:wechat
*/
function isWechat()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent, 'MicroMessenger') === false) {
return false;
} else {
return true;
}
}
http获取微信token
function http_get_token(){
// 如果是企业号用以下URL获取access_token
// $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$secret";
$res = json_decode(httpGet($url)); //httpGet ,curl get 请求
$access_token = $res->access_token;
if ($access_token) {
wx_set_token($access_token); //自定义token保存方式,可以存在数据库,也可以存在缓存。看情况吧。
}
return $access_token;
}
发送微信模板信息
/**
* $wechat_token
* $post_data 微信的模板数据
*/
function send_wechat($wechat_token,$post_data){
$ch = curl_init();
$access_token = wx_get_token(); //自定义token获取方式,从数据库、缓存中获取。
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
$post_data = json_encode($post_data);
curl_setopt($ch , CURLOPT_URL , $url);
curl_setopt($ch , CURLOPT_HEADER , 0 );
curl_setopt( $ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch , CURLOPT_POSTFIELDS , $post_data);
$rst = curl_exec( $ch );
$response = json_decode($rst);
if($response->errcode==40001){
$access_token = http_get_token();
$url2 = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
curl_setopt($ch , CURLOPT_URL , $url2);
curl_setopt($ch , CURLOPT_HEADER , 0 );
curl_setopt( $ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch , CURLOPT_POSTFIELDS , $post_data);
$rst = curl_exec( $ch );
}
curl_close( $ch );return $rst;
}
用户关注公众号
function wx_user_focuse($openId = null){
if($openId==null)return false;
$accessToken = wx_get_token();
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$accessToken."&openid=".$openId."&lang=zh_CN";
$res = json_decode(httpGet($url));
if($res->errcode==40001){
$accessToken = http_get_token();
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$accessToken."&openid=".$openId."&lang=zh_CN";
$res = json_decode(httpGet($url));
}
if($res->subscribe){
return true;
}else{
return false;
}
}
拉取公众号关注列表
function get_wechat_users(){
$ch = curl_init();
$access_token = wx_get_token();
$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" . $access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURL_SSLVERSION_SSL, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$rst = curl_exec( $ch );
$response = json_decode($rst);
if($response->errcode==40001){
$access_token = http_get_token();
$url2 = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" . $access_token;
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURL_SSLVERSION_SSL, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$rst = curl_exec( $ch );
$response = json_decode($rst);
}
curl_close( $ch );
return $response;
}