php+ Micro-communication interface development of WeChat distribution platform split explain (a) WeChat Oauth2 interface

Source: Internet
Author: User
Tags openid
me a IT a laborer, not a master is not a great God but hope to become Lei Feng. No god horse writing, only will grumble, explain not clear can continue to ask questions in the case of time permitting I will try to answer.

This article does not provide a full set of system source code, will only open part of the source code, talk about the development of the experience, ideas, questions answered, hope to help novice, as for the master and the great God to pass or leave valuable comments in this thanked.


No more nonsense to start getting to the point.

Basic article

PHP Current Mainstream WEB There is no doubt about developing languages. Interface is mainly used to authorize login interface,jssdk sharing interface, payment interface, for a series of interfaces must complain about the hole everywhere. Distribution systems are a marketing model that will be explained later. First of all, the first step login (oauth2 third-party licensing interface), the personal view of the more important one, the advantages of high user experience, high security, for the back of the distribution system to lay a convenient foundation, Disadvantages can only be accessed in the built-in browser for other ways to promote the promotion is not convenient, only with two-dimensional code (generation of links form the QR code on the Internet a lot of third-party tools, you can also use the program to achieve, interested can leave a message).

first look at the legendary API Interface documentation, website document address http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

Use this interface must require a certified service number, no case can apply for a test account experience but not promote

About the page authorization callback domain name description

document writing is clear, in the case of no third-party platform, need to configure the project needs to obtain user information path, one or two .... Domain names are available. This is quite clear.

Look down the first time did not understand, the second time did not understand ... The project was developed a year ago at the time of the document is more primitive than it is now and the information available on the Web very few official no instance, fortunately finally from an article to find clues is made, complain about

Other Notes

introduce several parameters first AppID These two parameters are used by calls to and Appsecret can find many interfaces in the public platform.

The most important parameter is also the purpose of invoking the authorization interface, OpenID, which is a number pair should be the only indicator of the public platform, get it can think that the user has logged in, behind all the business process and database records are built around it.

here's how to get OpenID and basic user information such as avatar nickname City Sex, this is a comparison of egg pain process, so many steps to play with us, right? Find a reason for your safety. I'm complaining again.

In three steps

1 Get code, what is code??? Don't know anyway get to it to proceed to the next

Interface Address: Https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&resp /SPAN>

AppID just raised in the public platform can be found, Redirect_uri is the point it is again the user sends a request to the server through the above interface address, the server through GET way to pass a parameter Code return to Redirect_uri, with $_get way to accept it. Scope is also a more important parameter. He has two types of snsapi_base,snsapi_userinfo , say the difference if you only want to achieve login OpenID then use Snsapi_base, The advantage of this is that users do not know when they visit that you have crawled his OpenID . In addition to the use of snsapi_userinfo can crawl the OpenID , but also to capture a lot more user information, now many applications need avatars and nicknames so straight to use snsapi_userinfo, the disadvantage is that the page that will first pop up authorization is allowed to go through the user's consent before the next operation


2. get OpenID and access_token

by just getting the Code as well AppID and the appsecret

Interface: https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=code& Grant_type=authorization_code

returned is the JSON format of Data

3 get avatar nickname, etc. ...

through OpenID and the Access_token

Interface :Https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

returned is the JSON format of Data

Parameters Lang return English data by default when not written

Instance code fragment

a.php

$url = ' http://www.xxx.com/b.php ';

$url =urlencode ($url);

$href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri= $url &resp/span>

@header ("Location: $href");

b.php

$code =$_get[' code ';

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

$js/span>

$jsonstr = Json_decode ($jsonstr, true);

$access _token= $jsonstr [' Access_token '];

$openid = $jsonstr [' OpenID '];

$userurl = ' https://api.weixin.qq.com/sns/userinfo?access_token= '. $access _token. ' &openid= '. $openid;

$userinfo =file_get_contents ($userurl);

$userinfo = Json_decode ($userinfo, true);

Well, it's just a realization .... But after a period of time to run crawl information is not stable will appear a part of the user did not get the Avatar and nickname for a long time do not know where the problem appears , where?????

Oh, come on . file_get_contents The problem with this function is to use file_get_contents Although it can be achieved but the effect of the performance is extremely unstable, and later switched to Curl It's a lot more stable, but not. 100% capture a variety of factors caused by, but basically within the acceptable range.

today's time is limited, the definition of distribution system, access to the user's avatar nickname and other information there is another way to do not need to eject the authorization interface, Unionid Multi-public platform development needs to use, to achieve a persistent login state needs Cookies , and the shopping cart implementation of the mall part of the distribution system client will be explained in the second time.

system access path QR code One, we can first look at only access, you can ask questions including system architecture, front-end JS The implementation of scripts and a series of functional modules, I will change the next update according to the needs of most people




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the Php+ interface development of the distribution platform split explained (a) Oauth2 interface, including the aspects of the content, I hope the PHP tutorial interested in a friend to help.

  • 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.