微信开发-php方法总结

  • 检验当前浏览器是否是微信环境
  • 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值