WeChat Public number Click menu to open and login micro-station implementation method, public _php tutorial

Source: Internet
Author: User
Tags openid

Public number Click on the menu to open and login to the micro-station implementation method, the public


The example of this article describes how to open and log in to a micro-station by clicking the Public number menu. Share to everyone for your reference. The specific analysis is as follows:

Overall, the public number click on the menu to open and login micro-station implementation of the step is more complex, but a lot of micro-stations in their own use, this article to summarize, I believe it can bring a certain reference value.

Most micro-stations now use the user's OpenID for automatic login. In my previous development, the user by clicking on a menu, the public number returned a text, the user click on the text to automatically log in to the micro-station. But if you have a high-level interface, you can implement the Click menu, open the Web page will be able to access the OpenID, automatic login.
As mentioned here, it is necessary to have the privilege of the Advanced Interface (service number, enterprise number) to open the developer mode.

1. Setting the callback Address

In the public platform backstage "Developer Center" find "OAuth2.0 page authorization" under "Advanced Interface", followed by a "modification", click will pop up to fill in the Callback Address dialog box. For details on how to authorize, please click here to learn. The "modify" of this place can only occur if the Advanced interface permission is obtained.
Note that here is the domain name, not with the URL, and the explanation is very clear, "Authorization callback domain name configuration specification for the whole domain name", that is, with the WWW and not with a different two domain names. So I'm going to fill in the domain name here.

2. Create a menu

The Create menu can be created from the background of your micro-station, or it can be created through the public platform if the developer mode is not turned on.
The menu uses the click-to-open link mode, which is view mode. If you are using developer mode, you can create a public menu (developer documentation) by submitting the following code:
Copy CodeThe code is as follows: {
"Button": [
{
"Type": "View",
"Name": "Login to micro-station",
"url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid={in the public platform backstage get this appid}&redirect_uri={ You fill in the callback domain under the address}&response_type=code&scope=snsapi_base&state=1#wechat_redirect "
}]
}
Code 1 to submit the menu code, the following to use the
The AppID location is the "Developer Center" where you fill in the callback address. Here we use PHP to implement the menu submission:
Copy CodeThe code is as follows: <?php
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);//Get the credentials, you need to define APP_ID and APP_SCR (app key), this is also in the public platform backstage Developer Center to find
if ($ACCESS _list[' access_token ']!= ') {
$access _token = $ACCESS _list[' access_token '];//get to Access_token
$data = ' Paste the above code 1 here ';
$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 (' Create custom menu successfully! ');
}
else {
Die (' Create custom menu failed! ');
}
}
else {
Die (' Create failed, AppID or Appsecret fill error ');
}
}
Create_menu ();
?>
Code 2 using PHP to create the public number menu

Code 2 is actually a bit redundant, and the core is marked in red. In this way, your public number should soon be seen creating a "login micro-Station" menu. Click on this menu to implement login micro-station.
If you don't need PHP, you can simply write a link in the menu customization in the background of the public platform.

In this place, choose how to open a link to create a menu. OK, then put the above link in:

Https://open.weixin.qq.com/connect/oauth2/authorize?appid={in the public platform to get this appid}&redirect_uri={you fill in the address of the callback domain} &response_type=code&scope=snsapi_base&state=1#wechat_redirect

You can create a menu.
Of course, you probably just need to add this link to your own admin background.

3. Get OpenID on the callback page

Careful you may have found that the above link address contains parameter scope=snsapi_base, not scope=snsapi_userinfo, because the use of the former does not require the user to click on an Authorization button, jump directly to the callback page, and the latter need to click the authorization button, However, click on the Authorization button is good, one can not pay attention to the public number can also be authorized, second, authorized to obtain some information about the user, such as nickname, Gender, location. But we are going to use OpenID to log in, so we can choose the former directly.

After clicking on the menu, the authorize will jump to the callback address you submitted (you need to remind me that the callback address is best not to take parameters, such as xxx/?callback=from_weixin, because jumping to your callback address also takes parameters, and this parameter is what you need). Jump to the following URL:
Callback Address/?code=code&state=1

The above code can get a code value by $_get[' code ', and using this value and AppID, OpenID and Access_token can be obtained.
The following is implemented in PHP:
Copy the Code code as follows: 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;
}
This allows you to get OpenID and Access_token, and with these values, we can also use the public platform's access to user basic information API interface to get basic user information.

It is hoped that this article will help you to develop PHP-based public numbers.

http://www.bkjia.com/PHPjc/911901.html www.bkjia.com true http://www.bkjia.com/PHPjc/911901.html techarticle Public Number Click on the menu to open and log in to the micro-station implementation method, the public This article describes the public number click on the menu to open and login to the micro-station implementation method. Share to the big ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.