亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
微信公眾號點擊菜單即可打開并登錄微站的實現(xiàn)方法,公眾
Home php教程 php手冊 微信公眾號點擊菜單即可打開并登錄微站的實現(xiàn)方法,公眾

微信公眾號點擊菜單即可打開并登錄微站的實現(xiàn)方法,公眾

Jun 13, 2016 am 09:21 AM
WeChat Log in

微信公眾號點擊菜單即可打開并登錄微站的實現(xiàn)方法,公眾

本文實例講述了微信公眾號點擊菜單即可打開并登錄微站的實現(xiàn)方法。分享給大家供大家參考。具體分析如下:

總體來說,微信公眾號點擊菜單即可打開并登錄微站實現(xiàn)步驟比較復(fù)雜,但很多微站在己用上了,本文對此進行整理歸納,相信可以給大家?guī)硪欢ǖ膮⒖冀梃b價值。

現(xiàn)在大部分微站都通過用戶的微信openid來實現(xiàn)自動登錄。在我之前的開發(fā)中,用戶通過點擊一個菜單,公眾號返回一個圖文,用戶點擊這個圖文才可以自動登錄微站。但是如果你擁有高級接口,就可以實現(xiàn)點擊菜單,打開網(wǎng)頁就能獲取這個openid,實現(xiàn)自動登錄。
這里已經(jīng)提到,必須要擁有高級接口的權(quán)限(服務(wù)號、企業(yè)號),開啟了開發(fā)者模式。

1.設(shè)置回調(diào)地址

在微信公眾平臺后臺“開發(fā)者中心”中找到“高級接口”下的“OAuth2.0網(wǎng)頁授權(quán)”,后面有一個“修改”,點擊之后就會彈出填寫回調(diào)地址的對話框。具體如何授權(quán),請點擊這里學(xué)習(xí)。只有獲得高級接口權(quán)限后,才能出現(xiàn)這個地方的“修改”。
注意,這里填寫的是域名,不是帶的網(wǎng)址,而且解釋中很清楚,“授權(quán)回調(diào)域名配置規(guī)范為全域名”,也就是說帶www和不帶是不同的兩個域名。因此我這里要填寫如下圖中的域名。

2. 創(chuàng)建菜單

創(chuàng)建菜單可以通過你的微站后臺創(chuàng)建,如果沒有開啟開發(fā)者模式,也可以通過微信公眾平臺后臺創(chuàng)建。
菜單使用點擊打開鏈接的模式,也就是view模式。如果你是使用開發(fā)者模式,通過向微信提交如下代碼,即可創(chuàng)建公眾號菜單(開發(fā)者文檔):

復(fù)制代碼 代碼如下:

{
???? "button":[
???? {
????????? "type":"view",
????????? "name":"登錄微站",
????????? "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid={在微信公眾平臺后臺獲取這個APPID}&redirect_uri={你填寫的回調(diào)域名下的地址}&response_type=code&scope=snsapi_base&state=1#wechat_redirect"
????? }]
}


代碼1 要提交的菜單代碼,下面要用到
APPID的獲取位置就是上面你填寫回調(diào)地址的那個“開發(fā)者中心”。下面我們用PHP來實現(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)用密鑰),這個也是在微信公眾平臺后臺開發(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填寫錯誤');
? }
}
create_menu();
?>


代碼2 用PHP來創(chuàng)建微信公眾號菜單

代碼2其實有點冗余了,核心部分用紅色標(biāo)出來了。就這樣,你的微信公眾號中應(yīng)該很快就可以看到創(chuàng)建了一個“登錄微站”的菜單。點擊這個菜單就可以實現(xiàn)登錄微站了。
如果你不需要PHP,可以直接在微信公眾平臺后臺的菜單自定義中寫鏈接就可以了。

在上圖中的這個地方,選擇打開鏈接的方式創(chuàng)建菜單。OK,接下來把上面那個鏈接放進去:

https://open.weixin.qq.com/connect/oauth2/authorize?appid={在微信公眾平臺后臺獲取這個APPID}&redirect_uri={你填寫的回調(diào)域名下的地址}&response_type=code&scope=snsapi_base&state=1#wechat_redirect

創(chuàng)建菜單就可以了。
當(dāng)然,你也有可能只需要在你自己的微信管理后臺加入這個鏈接就可以了。

3.在回調(diào)頁獲取openid

細心的你可能已經(jīng)發(fā)現(xiàn)了,上面的鏈接地址中含有參數(shù)scope=snsapi_base,而非scope=snsapi_userinfo,因為使用前者不需要用戶點擊一個授權(quán)按鈕,直接跳轉(zhuǎn)到回調(diào)頁面,而后者需要點擊授權(quán)按鈕,不過點擊授權(quán)按鈕有好處,一是可以在沒有關(guān)注公眾號的情況下也可以授權(quán),二是授權(quán)后可以獲得用戶的一些信息,如昵稱、性別、所在地。但是我們是為了利用openid進行登錄,所以直接選擇前者就可以了。

點擊菜單之后,經(jīng)過微信authorize的處理,會跳轉(zhuǎn)到你提交的回調(diào)地址(這里需要提醒,回調(diào)地址最好不要帶參數(shù),例如xxx/?callback=from_weixin,因為微信跳轉(zhuǎn)到你的回調(diào)地址也要帶參數(shù),而這個參數(shù)就你需要的)。微信跳轉(zhuǎn)到如下URL:
回調(diào)地址/?code=CODE&state=1

上面代碼可以通過$_GET['code']獲得一個CODE值,利用這個CODE值和appid,可以獲得openid和access_token。
下面再用PHP來實現(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ā)有所幫助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
TikTok web version entrance login link address https TikTok web version entrance website free TikTok web version entrance login link address https TikTok web version entrance website free May 22, 2025 pm 04:24 PM

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.

Copy comics (official website entrance)_Copy comics (nba) genuine online reading portal Copy comics (official website entrance)_Copy comics (nba) genuine online reading portal Jun 05, 2025 pm 04:12 PM

Copying comics is undoubtedly a treasure that cannot be missed. Here you can find basketball comics in various styles, from passionate and inspiring competitive stories to relaxed and humorous daily comedy. Whether you want to relive the classics or discover new works, copying comics can meet your needs. Through the authentic online reading portal provided by copy comics, you will bid farewell to the trouble of pirated resources, enjoy a high-definition and smooth reading experience, and can support your favorite comic authors and contribute to the development of authentic comics.

Which is better, uc browser or qq browser? In-depth comparison and evaluation of uc and qq browsers Which is better, uc browser or qq browser? In-depth comparison and evaluation of uc and qq browsers May 22, 2025 pm 08:33 PM

Choosing UC browser or QQ browser depends on your needs: 1. UC browser is suitable for users who pursue fast loading and rich entertainment functions; 2. QQ browser is suitable for users who need stability and seamless connection with Tencent products.

Top 10 AI writing software rankings Recommended Which AI writing software is free Top 10 AI writing software rankings Recommended Which AI writing software is free Jun 04, 2025 pm 03:27 PM

Combining the latest industry trends and multi-dimensional evaluation data in 2025, the following are the top ten comprehensive AI writing software recommendations, covering mainstream scenarios such as general creation, academic research, and commercial marketing, while taking into account Chinese optimization and localization services:

Watch the official page of NIS comics online for free comics. The free entry website of NIS comics login page Watch the official page of NIS comics online for free comics. The free entry website of NIS comics login page Jun 12, 2025 pm 08:18 PM

Nice Comics, an immersive reading experience platform dedicated to creating for comic lovers, brings together a large number of high-quality comic resources at home and abroad. It is not only a comic reading platform, but also a community that connects comic artists and readers and shares comic culture. Through simple and intuitive interface design and powerful search functions, NES Comics allows you to easily find your favorite works and enjoy a smooth and comfortable reading experience. Say goodbye to the long waiting and tedious operations, enter the world of Nice comics immediately and start your comic journey!

Frog Man Online Viewing Entrance Man Frog Man (Web Page Entrance) Watch Online Frog Man Online Viewing Entrance Man Frog Man (Web Page Entrance) Watch Online Jun 12, 2025 pm 08:06 PM

Frogman Comics has become the first choice for many comic lovers with its rich and diverse comic resources and convenient and smooth online reading experience. It is like a vibrant pond, with fresh and interesting stories constantly emerging, waiting for you to discover and explore. Frog Man comics cover a variety of subjects, from passionate adventures to sweet love, from fantasy and science fiction to suspense reasoning, no matter which genre you like, you can find your favorite works here. Its simple and intuitive interface design allows you to easily get started, quickly find the comics you want to read, and immerse yourself in the exciting comic world.

Baozi Comics (Entrance)_ Baozi Comics (New Entrance) 2025 Baozi Comics (Entrance)_ Baozi Comics (New Entrance) 2025 Jun 05, 2025 pm 04:18 PM

Here, you can enjoy the vast ocean of comics and explore works of various themes and styles, from passionate young man comics to delicate and moving girl comics, from suspenseful and brain-burning mystery comics to relaxed and funny daily comics, there is everything, and there is always one that can touch your heartstrings. We not only have a large amount of genuine comic resources, but also constantly introduce and update the latest works to ensure that you can read your favorite comics as soon as possible.

b An latest registered address_How to register b An exchange b An latest registered address_How to register b An exchange May 26, 2025 pm 07:12 PM

The latest official website of 2025b Announce is: https://www.marketwebb.co/zh-CN/join?ref=507720986&type=wenzi; Binance Exchange is a global cryptocurrency exchange that serves 180 countries and regions including North America, Europe, Taiwan, the Middle East, Hong Kong, and Malaysia. It provides more than 600 cryptocurrencies and has 270 million registered users worldwide.

See all articles