微信公眾號點(diǎn)擊菜單即可打開并登錄微站的實(shí)現(xiàn)方法,公眾
Jun 13, 2016 am 09:21 AM微信公眾號點(diǎn)擊菜單即可打開并登錄微站的實(shí)現(xiàn)方法,公眾
本文實(shí)例講述了微信公眾號點(diǎn)擊菜單即可打開并登錄微站的實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:
總體來說,微信公眾號點(diǎn)擊菜單即可打開并登錄微站實(shí)現(xiàn)步驟比較復(fù)雜,但很多微站在己用上了,本文對此進(jìn)行整理歸納,相信可以給大家?guī)硪欢ǖ膮⒖冀梃b價(jià)值。
現(xiàn)在大部分微站都通過用戶的微信openid來實(shí)現(xiàn)自動登錄。在我之前的開發(fā)中,用戶通過點(diǎn)擊一個(gè)菜單,公眾號返回一個(gè)圖文,用戶點(diǎn)擊這個(gè)圖文才可以自動登錄微站。但是如果你擁有高級接口,就可以實(shí)現(xiàn)點(diǎn)擊菜單,打開網(wǎng)頁就能獲取這個(gè)openid,實(shí)現(xiàn)自動登錄。
這里已經(jīng)提到,必須要擁有高級接口的權(quán)限(服務(wù)號、企業(yè)號),開啟了開發(fā)者模式。
1.設(shè)置回調(diào)地址
在微信公眾平臺后臺“開發(fā)者中心”中找到“高級接口”下的“OAuth2.0網(wǎng)頁授權(quán)”,后面有一個(gè)“修改”,點(diǎn)擊之后就會彈出填寫回調(diào)地址的對話框。具體如何授權(quán),請點(diǎn)擊這里學(xué)習(xí)。只有獲得高級接口權(quán)限后,才能出現(xiàn)這個(gè)地方的“修改”。
注意,這里填寫的是域名,不是帶的網(wǎng)址,而且解釋中很清楚,“授權(quán)回調(diào)域名配置規(guī)范為全域名”,也就是說帶www和不帶是不同的兩個(gè)域名。因此我這里要填寫如下圖中的域名。
2. 創(chuàng)建菜單
創(chuàng)建菜單可以通過你的微站后臺創(chuàng)建,如果沒有開啟開發(fā)者模式,也可以通過微信公眾平臺后臺創(chuàng)建。
菜單使用點(diǎn)擊打開鏈接的模式,也就是view模式。如果你是使用開發(fā)者模式,通過向微信提交如下代碼,即可創(chuàng)建公眾號菜單(開發(fā)者文檔):
復(fù)制代碼 代碼如下:
{
???? "button":[
???? {
????????? "type":"view",
????????? "name":"登錄微站",
????????? "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid={在微信公眾平臺后臺獲取這個(gè)APPID}&redirect_uri={你填寫的回調(diào)域名下的地址}&response_type=code&scope=snsapi_base&state=1#wechat_redirect"
????? }]
}
代碼1 要提交的菜單代碼,下面要用到
APPID的獲取位置就是上面你填寫回調(diào)地址的那個(gè)“開發(fā)者中心”。下面我們用PHP來實(shí)現(xiàn)一下菜單提交:
復(fù)制代碼 代碼如下:
function curl_info($appid,$secret) {
? $ch = curl_init();
? curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret);
? curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
? curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
? curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
? curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
? curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
? curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
? // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
? curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
? $tmpInfo = curl_exec($ch);
? if (curl_errno($ch)) {?
??? echo 'Errno'.curl_error($ch);
? }
? curl_close($ch);
? $arr= json_decode($tmpInfo,true);
? return $arr;
}
function curl_menu($ACCESS_TOKEN,$data) {
? $ch = curl_init();
? curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$ACCESS_TOKEN);
? curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
? curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
? curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
? curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
? curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
? curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
? curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
? curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
? $tmpInfo = curl_exec($ch);
? if (curl_errno($ch)) {
??? echo 'Errno'.curl_error($ch);
? }
? curl_close($ch);
? $arr= json_decode($tmpInfo,true);
? return $arr;
}
function creat_menu() {
? $ACCESS_LIST= curl_info(APP_ID,APP_SCR);//獲取到的憑證,你需要自己define APP_ID和APP_SCR(應(yīng)用密鑰),這個(gè)也是在微信公眾平臺后臺開發(fā)者中心找
? if($ACCESS_LIST['access_token']!='') {
??? $access_token = $ACCESS_LIST['access_token'];//獲取到ACCESS_TOKEN
??? $data = '把上面代碼1拷貝黏貼在這里';
??? $msg = curl_menu($access_token,preg_replace("#u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '1'))", $data));
??? if($msg['errmsg']=='ok') {
????? die('創(chuàng)建自定義菜單成功!');
??? }
??? else {
????? die('創(chuàng)建自定義菜單失敗!');
??? }
? }
? else {
??? die('創(chuàng)建失敗,微信AppId或微信AppSecret填寫錯(cuò)誤');
? }
}
create_menu();
?>
代碼2 用PHP來創(chuàng)建微信公眾號菜單
代碼2其實(shí)有點(diǎn)冗余了,核心部分用紅色標(biāo)出來了。就這樣,你的微信公眾號中應(yīng)該很快就可以看到創(chuàng)建了一個(gè)“登錄微站”的菜單。點(diǎn)擊這個(gè)菜單就可以實(shí)現(xiàn)登錄微站了。
如果你不需要PHP,可以直接在微信公眾平臺后臺的菜單自定義中寫鏈接就可以了。
在上圖中的這個(gè)地方,選擇打開鏈接的方式創(chuàng)建菜單。OK,接下來把上面那個(gè)鏈接放進(jìn)去:
https://open.weixin.qq.com/connect/oauth2/authorize?appid={在微信公眾平臺后臺獲取這個(gè)APPID}&redirect_uri={你填寫的回調(diào)域名下的地址}&response_type=code&scope=snsapi_base&state=1#wechat_redirect
創(chuàng)建菜單就可以了。
當(dāng)然,你也有可能只需要在你自己的微信管理后臺加入這個(gè)鏈接就可以了。
3.在回調(diào)頁獲取openid
細(xì)心的你可能已經(jīng)發(fā)現(xiàn)了,上面的鏈接地址中含有參數(shù)scope=snsapi_base,而非scope=snsapi_userinfo,因?yàn)槭褂们罢卟恍枰脩酎c(diǎn)擊一個(gè)授權(quán)按鈕,直接跳轉(zhuǎn)到回調(diào)頁面,而后者需要點(diǎn)擊授權(quán)按鈕,不過點(diǎn)擊授權(quán)按鈕有好處,一是可以在沒有關(guān)注公眾號的情況下也可以授權(quán),二是授權(quán)后可以獲得用戶的一些信息,如昵稱、性別、所在地。但是我們是為了利用openid進(jìn)行登錄,所以直接選擇前者就可以了。
點(diǎn)擊菜單之后,經(jīng)過微信authorize的處理,會跳轉(zhuǎn)到你提交的回調(diào)地址(這里需要提醒,回調(diào)地址最好不要帶參數(shù),例如xxx/?callback=from_weixin,因?yàn)槲⑿盘D(zhuǎn)到你的回調(diào)地址也要帶參數(shù),而這個(gè)參數(shù)就你需要的)。微信跳轉(zhuǎn)到如下URL:
回調(diào)地址/?code=CODE&state=1
上面代碼可以通過$_GET['code']獲得一個(gè)CODE值,利用這個(gè)CODE值和appid,可以獲得openid和access_token。
下面再用PHP來實(shí)現(xiàn)以下:
復(fù)制代碼 代碼如下:
if($_GET['code']) {
? $code = $_GET['code'];
? $data = get_by_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSRC&code='.$code.'&grant_type=authorization_code');
? $data = json_decode($data);
? $openid = $data->openid;
? $access_token = $data->access_token;
}
function get_by_curl($url,$post = false){
??? $ch = curl_init();
??? curl_setopt($ch,CURLOPT_URL,$url);
??? curl_setopt($ch, CURLOPT_HEADER, 0);
??? curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
??? if($post){
??????? curl_setopt($ch, CURLOPT_POST, 1);
??????? curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
??? }
??? $result = curl_exec($ch);
??? curl_close($ch);
??? return $result;
}
這樣可以就可以獲得openid和access_token,利用這些值,我們還可以利用微信公眾平臺的獲取用戶基本信息api接口獲取用戶基本信息。
希望本文所述對大家基于PHP的微信公眾號開發(fā)有所幫助。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

The choice of H5 and applet depends on the requirements. For applications with cross-platform, rapid development and high scalability, choose H5; for applications with native experience, rich functions and platform dependencies, choose applets.

There are differences in the promotion methods of H5 and mini programs: platform dependence: H5 depends on the browser, and mini programs rely on specific platforms (such as WeChat). User experience: The H5 experience is poor, and the mini program provides a smooth experience similar to native applications. Communication method: H5 is spread through links, and mini programs are shared or searched through the platform. H5 promotion methods: social sharing, email marketing, QR code, SEO, paid advertising. Mini program promotion methods: platform promotion, social sharing, offline promotion, ASO, cooperation with other platforms.

The best cryptocurrency trading and analysis platforms include: 1. OKX: the world's number one in trading volume, supports multiple transactions, provides AI market analysis and on-chain data monitoring. 2. Binance: The world's largest exchange, providing in-depth market conditions and new currency first-time offerings. 3. Sesame Open Door: Known for spot trading and OTC channels, it provides automated trading strategies. 4. CoinMarketCap: an authoritative market data platform, covering 20,000 currencies. 5. CoinGecko: Known for community sentiment analysis, it provides DeFi and NFT trend monitoring. 6. Non-small account: a domestic market platform, providing analysis of linkage between A-shares and currency markets. 7. On-chain Finance: Focus on blockchain news and update in-depth reports every day. 8. Golden Finance: 24 small

The login portal for the Douyin web version is https://www.douyin.com/. The login steps include: 1. Open the browser; 2. Enter the URL https://www.douyin.com/; 3. Click the "Login" button and select the login method; 4. Enter the account password; 5. Complete login. The web version provides functions such as browsing, searching, interaction, uploading videos and personal homepage management, and has advantages such as large-screen experience, multi-tasking, convenient account management and data statistics.

10 top scams on cryptocurrency exchanges Common scams: fake exchanges, Ponzi capital trading, contract manipulation, fake coin phishing, customer service fraud, etc. Identification points: Check regulatory licenses, check contract addresses, and be wary of high-yield commitments Must be protected: Use only mainstream exchanges (Binance/Coinbase) Enable hardware wallet Reject share private key/verification code Deal with fraud: take screenshots immediately, freeze assets, report on the platform, and report to the police Core principle: Any request for password/transfer is a fraud!

Binance C2C transactions allow users to buy and sell cryptocurrencies directly, and pay attention to the risks of counterparty, payment and price fluctuations. Choosing high-credit users and secure payment methods can reduce risks.
