How to implement the WeChat public account to open and log in to the microsite by clicking on the menu, public_PHP tutorial

WBOY
Release: 2016-07-13 10:14:11
Original
831 people have browsed it

How to implement the WeChat public account to open and log in to the microsite by clicking on the menu, public

The example in this article describes how to implement the WeChat public account to open and log in to the microsite by clicking on the menu. Share it with everyone for your reference. The specific analysis is as follows:

Generally speaking, the steps to implement the WeChat official account by clicking on the menu to open and log in to the microsite are relatively complicated, but many microsites have been used. This article summarizes this, and I believe it can bring some reference to everyone. Reference value.

Nowadays, most microsites realize automatic login through the user’s WeChat openid. In my previous development, the user clicked on a menu, and the official account returned an image and text. Only when the user clicked on this image and text could they automatically log in to the microsite. But if you have an advanced interface, you can click on the menu and open the web page to get the openid and realize automatic login.
As mentioned here, you must have the permissions of the advanced interface (service account, enterprise account) and turn on the developer mode.

1. Set callback address

In the "Developer Center" of the WeChat public platform background, find "OAuth2.0 Web Authorization" under "Advanced Interface". There is a "Modify" after it. After clicking, a dialog box for filling in the callback address will pop up. For details on how to authorize, please click here to learn. Only after obtaining the advanced interface permission can the "modification" of this place appear.
Note that the domain name filled in here is the domain name, not the URL, and the explanation is very clear, "The authorization callback domain name configuration specification is the full domain name", which means that the domain name with www and without it are two different domain names. Therefore, I need to fill in the domain name as shown below.

2. Create menu

The creation menu can be created through the backend of your microsite. If the developer mode is not turned on, it can also be created through the backend of the WeChat public platform.
The menu uses the click-to-open link mode, which is the view mode. If you are using developer mode, you can create a public account menu (developer documentation) by submitting the following code to WeChat:

Copy code The code is as follows:
{
"button":[
{
"type":"view",
"name":"Log in to the microsite",
"url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid={Get this APPID in the WeChat public platform background}&redirect_uri={The address under the callback domain name you filled in}&response_type=code&scope =snsapi_base&state=1#wechat_redirect"
}]
}

Code 1 Menu code to be submitted,
will be used below The location to obtain the APPID is the "Developer Center" where you filled in the callback address above. Next we use PHP to implement menu submission:
Copy code The code is as follows:
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(应用密钥),这个也是在微信公众平台后台开发者中心找
  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('创建自定义菜单成功!');
    }
    else {
      die('创建自定义菜单失败!');
    }
  }
  else {
    die('创建失败,微信AppId或微信AppSecret填写错误');
  }
}
create_menu();
?>

Code 2 Use PHP to create WeChat public account menu

Code 2 is actually a bit redundant, the core part is highlighted in red. In this way, you should soon see a "Login to Microsite" menu created in your WeChat official account. Click this menu to log in to the microsite.
If you don’t need PHP, you can just write the link directly in the menu customization in the background of the WeChat public platform.

In this place in the picture above, choose to open the link to create the menu. OK, now put the link above:

https://open.weixin.qq.com/connect/oauth2/authorize?appid={Get this APPID in the WeChat public platform background}&redirect_uri={The address under the callback domain name you filled in}&response_type=code&scope=snsapi_base&state =1#wechat_redirect

Just create the menu.
Of course, you may also just need to add this link to your own WeChat management background.

3. Get the openid on the callback page

If you are careful, you may have noticed that the above link address contains the parameter scope=snsapi_base instead of scope=snsapi_userinfo, because using the former does not require the user to click an authorization button and jump directly to the callback page, while the latter requires clicking Authorization button, but there are advantages to clicking the authorization button. First, you can authorize without following the official account. Second, after authorization, you can get some information about the user, such as nickname, gender, and location. But we are using openid to log in, so just choose the former.

After clicking the menu, after WeChat authorize processing, it will jump to the callback address you submitted (it needs to be reminded here that the callback address is best not to take parameters, such as xxx/?callback=from_weixin, because WeChat jumps to your The callback address also needs parameters, and this parameter is what you need). WeChat jumps to the following URL:
Callback address/?code=CODE&state=1

The above code can obtain a CODE value through $_GET['code']. Using this CODE value and appid, openid and access_token can be obtained.
Next, use PHP to implement the following:

Copy code The code is 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;
}

In this way, openid and access_token can be obtained. Using these values, we can also use the WeChat public platform's API interface to obtain basic user information.

I hope this article will be helpful to everyone in developing WeChat public accounts based on PHP.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/911901.htmlTechArticleWeChat public account clicks the menu to open and log in to the microsite. The public example in this article tells the WeChat public account Click the menu to open and log in to the microsite. Share with everyone...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!