您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
php微信开放平台代授权获取公众号网页授权用户信息-流程
发布时间:2022-06-07 20:59:05编辑:雪饮阅读()
虽然说有easywechat这样简单的sdk,但是在某些情况下有些环境过于老,用起来还是不那么方便的,更为灵活的使用还是要理解具体的业务流程。
这里说的业务流程是指以第三方的方式去带代为获取openid以及昵称之类的用户信息的实现思路。
public function step1(){
//要获取的用户的openid所在的公众号
$officialAccount_appid="wx9b77b8e0ba953162";
$redirect_uri=urlencode("https://shengke.bhzlkj.com/api/h5/open_platform/step2.html");
$state=1;
//第三方平台appid
$component_appid="wxcce813d0295ee283";
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$officialAccount_appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=".$state."&connect_redirect=1&component_appid=".$component_appid;
$url.="#wechat_redirect";
Header("Location: $url");
/*
*
*
*
*
* */
}
public function step2(){
$params=$this->request->get();
$officialAccount_appid="wx9b77b8e0ba953162";
$component_appid="wxcce813d0295ee283";
$getComponentAccessToken2Res=$this->getComponentAccessToken2(true);
$omponent_access_token=$getComponentAccessToken2Res["component_access_token"];
//浏览器直接访问有白名单问题,这里可以统一由服务器来请求
$url="https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=".$officialAccount_appid."&code=".$params["code"]."&grant_type=authorization_code&component_appid=".$component_appid."&component_access_token=".$omponent_access_token;
$client = new Client();
//这一步已经能拿到openid了
$response = $client->request('GET', $url);
$body = $response->getBody();//获取相应体
$contents = $body->getContents();//获取目标页面
$contents_to_arr=json_decode($contents,true);
//这一步可以获取更全面的用户信息
$url2="https://api.weixin.qq.com/sns/userinfo?access_token=".$contents_to_arr["access_token"]."&openid=".$contents_to_arr["openid"]."&lang=zh_CN";
$client = new Client();
$response = $client->request('GET', $url2);
$body = $response->getBody();//获取相应体
$contents = $body->getContents();//获取目标页面
$contents_to_arr2=json_decode($contents,true);
var_dump($contents_to_arr2);
}
关键字词:php,开放平台,代授权,流程