日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php微信网页授权登录代码,php微信网页授权代码(获取用户信息)

發(fā)布時間:2023/12/15 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php微信网页授权登录代码,php微信网页授权代码(获取用户信息) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.填寫授權(quán)回調(diào)頁面的域名 ?(注意只有服務號才可以頁面授權(quán))

登錄公眾平臺-->開發(fā)者中心-->接口權(quán)限表

2.代碼如下

scope為snsapi_base 那么用戶必須是關注了公眾號才能取得信息

index.php

//scope=snsapi_base 實例

$appid='你的AppId';

$redirect_uri = urlencode ( 'http://你的域名/getUserInfo.php' );

$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

header("Location:".$url);

getUserInfo.php

$appid = "你的AppId";

$secret = "你的AppSecret";

$code = $_GET["code"];

//第一步:取全局access_token

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

$token = getJson($url);

//第二步:取得openid

$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";

$oauth2 = getJson($oauth2Url);

//第三步:根據(jù)全局access_token和openid查詢用戶信息

$access_token = $token["access_token"];

$openid = $oauth2['openid'];

$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";

$userinfo = getJson($get_user_info_url);

//打印用戶信息

print_r($userinfo);

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);

}

scope為snsapi_userinfo 用戶不用關注公眾號,也能取到信息,但是會有一個界面讓用戶去點擊確認

index.php

$appid='你的AppId';

$redirect_uri = urlencode ( 'http://你的域名/getUserInfo.php' );

$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

header("Location:".$url);

getUserInfo.php

$appid = "你的AppId";

$secret = "你的AppSecret";

$code = $_GET["code"];

//第一步:取得openid

$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";

$oauth2 = getJson($oauth2Url);

//第二步:根據(jù)全局access_token和openid查詢用戶信息

$access_token = $oauth2["access_token"];

$openid = $oauth2['openid'];

$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";

$userinfo = getJson($get_user_info_url);

//打印用戶信息

print_r($userinfo);

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);

}

標簽: 微信

頂一下

(0)

0%

踩一下

(0)

0%

總結(jié)

以上是生活随笔為你收集整理的php微信网页授权登录代码,php微信网页授权代码(获取用户信息)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。