Micro-Credit public number click on the menu to open and login to the micro-station implementation method _php tips

Source: Internet
Author: User
Tags openid

This article describes the micro-letter public number click menu to open and login to the micro-station implementation method. Share to everyone for your reference. The specific analysis is as follows:

Generally speaking, micro-credit public number Click on the menu can be opened and login micro-station implementation steps are more complex, but many micro-stations in their own use, this article on the collation of induction, I believe that we can bring some reference value.

Now most of the micro-stations are through the user's micro-mail OpenID to achieve automatic login. In my previous development, the user clicks a menu, the public number returns a picture and text, the user clicks this picture and text can automatically login micro-station. But if you have an advanced interface, you can click on the menu, open the Web page can get this OpenID, to achieve automatic login.
It has been mentioned here that you have to have the privileges of the Advanced Interface (service number, enterprise number) to open the developer mode.

1. Set Callback Address

In the micro-credit public platform backstage "developer Center" under "Advanced Interface" under "OAuth2.0 Web page Authorization", followed by a "modified", after the click Will pop up to fill out the Callback Address dialog box. Specifically how to authorize, please click here to learn. The "modify" of this place can occur only after you have advanced interface permissions.
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 full domain name", that is, with the WWW and not with a different two domain names. So I am here to fill in the following image of the domain name.

2. Create Menu

Create menus can be created through your micro-station background, if you do not open the developer mode, can also be created through the micro-trust public platform background.
The menu uses the click-Open link mode, which is the view mode. If you are using the developer model, you can create the public number menu (developer documentation) by submitting the following code to the micro-mail:

Copy Code code as follows:
{
"Button": [
{
"Type": "View",
"Name": "Login to micro-station",
"url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid={in the background of the micro-trust public platform to 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 The menu code to be submitted, use the following
The AppID location is the "Developer Center" above where you fill out the callback address. Below we use PHP to implement the menu submission:
Copy Code code 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);//Obtain the voucher, you need to define APP_ID and APP_SCR (Application key), this is also in the micro-trust public platform Background 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 copy 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 a custom menu success! ');
}
else {
Die (' Create custom menu failed! ');
}
}
else {
Die (' Create failure, micro-letter AppID or micro-letter Appsecret fill error ');
}
}
Create_menu ();
?>

Code 2 using PHP to create a micro-public number menu

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

In this place in the diagram above, select the way to open the link to create the menu. OK, then put the above link in:

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

Creating a menu is OK.
Of course, you may just need to add this link to your own micro-trust admin.

3. Get OpenID on callback page

Careful you may have found that the link address above contains parameter scope=snsapi_base, rather than scope=snsapi_userinfo, because using the former does not require the user to click an authorization button, jump directly to the callback page, and the latter need to click the authorization button, However, it is good to click on the authorization button, one can not pay attention to the public number can also authorize, the second is authorized to get users of some information, such as nicknames, sex, location. But we are in order to use OpenID to log in, so direct selection of the former can be.

Click on the menu, after the micro-authorize processing, will jump to your submission of the callback address (here needs to be reminded that the callback address is best not with parameters, such as Xxx/?callback=from_weixin, because the micro-letter jump to your callback address also with parameters, And this parameter is what you need). Micro-mail jumps to the following URL:
Callback Address/?code=code&state=1

The code above can be obtained by $_get[' code ', using the code value and AppID to obtain OpenID and Access_token.
Here's how to do this with PHP:

Copy 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 for OpenID and access_token, using these values, we can also use the micro-trust public platform to obtain user basic information API interface to obtain user basic information.

I hope this article will help you with the development of micro-public number based on PHP.

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.