關(guān)于微信網(wǎng)頁(yè)授權(quán)
時(shí)間:2023-05-23 12:33:02 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-05-23 12:33:02 來(lái)源:網(wǎng)站運(yùn)營(yíng)
關(guān)于微信網(wǎng)頁(yè)授權(quán):我自己親測(cè)可行 下面貼代碼,希望能給需要幫助的人以幫助
class WeixinController
extends Controller{
public $appId =
'wx...';
public $appSecret =
'f3....';
public function getaccesstoken()
{
$js_code = $_GET[
'code'];
if($js_code){
//dump($js_code);
$url =
"https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appId&secret=$this->appSecret&code=$js_code
&grant_type=authorization_code";
//dump($url);
$res = WeixinController::
getJson($url);
$openid = $res[
'openid'];
$access_token = $res[
'access_token'];
$url1 =
"https://api.weixin.qq.com/sns/userinfo?access_token=$access_token
&openid=$openid
&lang=zh_CN ";
$date = WeixinController::
getJson($url1);
//dump($date);die;
$_SESSION[
'headImgUrl'] = $date[
'headimgurl'];
$_SESSION[
'nickName'] = $date[
'nickname'];
dump($_SESSION[
'headImgUrl']);
if(session(
'headImgUrl')){
$this->redirect(
'Login/login');
}
}
else{
echo 'code沒(méi)有相應(yīng)';
}
}
public static function getJson($url){
$ch =
curl_init();
curl_setopt($ch,
CURLOPT_URL, $url);
curl_setopt($ch,
CURLOPT_SSL_VERIFYPEER,
FALSE);
curl_setopt($ch,
CURLOPT_SSL_VERIFYHOST,
FALSE);
curl_setopt($ch,
CURLOPT_RETURNTRANSFER, 1);
$output =
curl_exec($ch);
curl_close($ch);
return json_decode($output,
true);
}
}