微信开发学习 OAUTH2.0 搭配事件推送

来源:互联网 发布:mac双系统 分区 编辑:程序博客网 时间:2024/05/16 23:41

先要实现这样的效果  

使用网页授权OAUTH2.0  之后微信自动推送一条信息 但是这里需要注意的是  

OAUTH2.0   也能获取 access_token

但是这个access_token 并不能用于基本的接口支持(无法用于发送消息)

所以需要重新申请 access_token

"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";

才能实现反馈

最终效果为 授权后反馈信息

//网页授权反馈function authorization($code){    echo $code;    $appid="";    $appsecret="";    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";    $json = file_get_contents($url);    $arr = json_decode($json, true);    $token = $arr['access_token'];    $openid = $arr['openid'];    //拿到token后就可以获取用户基本信息了    $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid ";    $json = file_get_contents($url); //获取微信用户基本信息    $arr = json_decode($json, true);    $name = $arr['nickname']; //昵称    $imgURL = $arr['headimgurl']; //头像地址    $sex = $arr['sex']; //性别    $province = $arr['province']; //用户个人资料填写的省份    $city = $arr['city']; //普通用户个人资料填写的城市    $country = $arr['country']; //国家,如中国为CN    echo $name;    echo $country;    // 到了这一步都是可以正常获取        //网上说 用户网页授权的access_token 和基础接入的 access_token 不是同样的,故需要重新获取    $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";    $json = file_get_contents($url);    $arr = json_decode($json, true);    $access_token = $arr['access_token'];    $txt = '{                     "touser":"'.$openid.'",                     "msgtype":"text",                     "text":{                                 "content":"Hello World"                             }                 }';    $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;    $result = https_post($url, $txt);    var_dump($result);}
0 0