微信常用API

1.网页授权 php/wx-sample.php

2.设置分享内容php/wx-sample.php

3.处理用户消息wx.php

微信授权登录

用户同意授权,获取code

在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
 

参数说明

参数是否必须说明

appid是公众号的唯一标识

redirect_uri是授权后重定向的回调链接地址,请使用urlencode对链接进行处理

response_type是返回类型,请填写code

scope是应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)

state否重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节

#wechat_redirect是无论直接打开还是做页面302重定向时候,必须带此参数

[demo]:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx35d17e1582ed4ab0&redirect_uri=http%3A%2F%2Fchina.bringmehope.org%2Fwp-admin%2Fthemes%2Fbmh%2Fwx.php&response_type=code&scope=snsapi_userinfo&state=sigstatus#wechat_redirect
 

授权成功后微信重定向到如下地址

http://china.bringmehope.org/wp-admin/themes/bmh/wx.php?code=031Zb2w12kUEI21ueyt12bf6w12Zb2w9&state=sigstatus

根据code参数获取access_token

https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx35d17e1582ed4ab0&secret=1cbe9bb6b6591097dd7b43481c5f4d43&code=041r9d0S04Lgnc2kjP3S0rBj0S0r9d0Q&grant_type=authorization_code 

[response]:

{"access_token":"t22OfbAfBbEMPonPQqbOhAd1ULyMxN3XHSYG0DN5DZkXdR-L0uMJ4C_YvJisSCXpHk_szKxh7OhCJJjs6vyJZAx71cG162Chl6OOvDlKvQc","expires_in":7200,"refresh_token":"XBHRNtBFFFtcWawO--RcDf77x5wVFOo8VYAqUeYI48_KpeJ14_wIUamdT-d_Av9ht8olIwateABuwEmDzmxEdMvLYVu5E4GYZ4DwRWMpuOg","openid":"o6HFmuGtadMNbuicrjpByl4gBeTA","scope":"snsapi_userinfo"}
 

刷新access_token(如果需要)

由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,refresh_token拥有较长的有效期(7天、30天、60天、90天),当refresh_token失效的后,需要用户重新授权。

获取第二步的refresh_token后,请求以下链接获取access_token: 

https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wx35d17e1582ed4ab0&grant_type=refresh_token&refresh_token=XBHRNtBFFFtcWawO--RcDf77x5wVFOo8VYAqUeYI48_KpeJ14_wIUamdT-d_Av9ht8olIwateABuwEmDzmxEdMvLYVu5E4GYZ4DwRWMpuOg

[response]:

{"openid":"o6HFmuGtadMNbuicrjpByl4gBeTA","access_token":"t22OfbAfBbEMPonPQqbOhAd1ULyMxN3XHSYG0DN5DZkXdR-L0uMJ4C_YvJisSCXpHk_szKxh7OhCJJjs6vyJZAx71cG162Chl6OOvDlKvQc","expires_in":7200,"refresh_token":"XBHRNtBFFFtcWawO--RcDf77x5wVFOo8VYAqUeYI48_KpeJ14_wIUamdT-d_Av9ht8olIwateABuwEmDzmxEdMvLYVu5E4GYZ4DwRWMpuOg","scope":"snsapi_base,snsapi_userinfo,"}
 

拉取用户信息(需scope为 snsapi_userinfo)

https://api.weixin.qq.com/sns/userinfo?access_token=t22OfbAfBbEMPonPQqbOhAd1ULyMxN3XHSYG0DN5DZkXdR-L0uMJ4C_YvJisSCXpHk_szKxh7OhCJJjs6vyJZAx71cG162Chl6OOvDlKvQc&openid=o6HFmuGtadMNbuicrjpByl4gBeTA&lang=zh_CN

[response]:

{"openid":"o6HFmuGtadMNbuicrjpByl4gBeTA","nickname":"漂浮的云","sex":1,"language":"zh_CN","city":"深圳","province":"广东","country":"中国","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/f8OjTfQlh73iaK0lct9FHKx8icqY6leclpsy485XpezqDibibcBfIXlwmKL3WIEVUJyVM1rvn9iaDjr7YoOLKLwj77E64f9ZV22z1\/0","privilege":[]}
 

参数描述

openid用户的唯一标识

nickname用户昵称

sex用户的性别,值为1时是男性,值为2时是女性,值为0时是未知

province用户个人资料填写的省份

city普通用户个人资料填写的城市

country国家,如中国为CN

headimgurl用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。

privilege用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)

unionid只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。详见:获取用户个人信息(UnionID机制)

检验授权凭证(access_token)是否有效

https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID

[demo]:

https://api.weixin.qq.com/sns/auth?access_token=t22OfbAfBbEMPonPQqbOhAd1ULyMxN3XHSYG0DN5DZkXdR-L0uMJ4C_YvJisSCXpHk_szKxh7OhCJJjs6vyJZAx71cG162Chl6OOvDlKvQc&openid=o6HFmuGtadMNbuicrjpByl4gBeTA

[response]:

(正常)

{"errcode":0,"errmsg":"ok"}

(失败)

{"errcode":40003,"errmsg":"invalid openid, hints: [ req_id: R2xLVa0634ns80 ]"}

相关推荐