无极网站建设质量,h5小游戏源码大全,软件商店网页版,手机网站开发常用工具使用php实现微信登录其实并不难#xff0c;可以简单地分为三步进行。
第一步#xff1a;用户同意授权#xff0c;获取code
//微信登录public function wxlogin(){$appid ;$secret ;$strhttp://***.***.com/getToken;$redirect_uriu… 使用php实现微信登录其实并不难可以简单地分为三步进行。
第一步用户同意授权获取code
//微信登录public function wxlogin(){$appid ;$secret ;$strhttp://***.***.com/getToken;$redirect_uriurlencode($str);//通过code获得 access_token openid$urlhttps://open.weixin.qq.com/connect/oauth2/authorize?appid.$appid.redirect_uri.$redirect_uri.response_typecodescopesnsapi_userinfostateSTATE#wechat_redirect;header(Location: . $url);}
第二步通过code换取网页授权access_token
public function getToken(){$code $_GET[code];$appid ;$secret ;//通过code获得 access_token openid$urlhttps://api.weixin.qq.com/sns/oauth2/access_token?appid.$appid.secret . $secret . code . $code . grant_typeauthorization_code;$jsonResult $this-https_request($url);$resultArray json_decode($jsonResult, true);$access_token $resultArray[access_token];$openid $resultArray[openid];//第三步 获取用户信息//通过access_token openid 获得用户所有信息,结果全部存储在$infoArray里,后面再写自己的代码逻辑$infoUrl https://api.weixin.qq.com/sns/userinfo?access_token . $access_token . openid . $openid.langzh_CN;$infoResult $this-https_request($infoUrl);$infoArray json_decode($infoResult, true);if($infoArray[unionid]){}}
附加代码中用到的方法
// An highlighted blockfunction https_request($url, $data null){$curl curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);if (!empty($data)){curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, $data);}curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$output curl_exec($curl);curl_close($curl);return $output;}