WeChat public number uses PHP to get authorized user information of H5 web page.

1, Prepare tools

Service number (subscription number can't get information of authorized user of web page), domain name, server

Two, configure the public number.

1. Get developer id and password:


2. Set the authorized domain name of the web page:

Business domain name and js interface security domain name, do not need to configure, just configure the authorized domain name of the web page!



3. Configure the certificate, and put the downloaded Certificate in the root directory of the domain name

If the browser can access the file, the configuration is successful:

3, Code implementation

Bloggers use the tp3 framework

Process 1: access the method, pull up the authorization interface of the web page, and if you agree, take the parameters to jump to the callback address

myurl this address is the callback address I set randomly. This address does not need to be specified on the public address!!!

	public function webpage_authorization()
    {
        $appid="Access to WeChat public address"; //AppID
        $key="Access to WeChat public address"; //AppSecret
        
        $myurl=urlencode("http://baidu.com/index/redirect_url"); //Callback domain name
        $wxurl='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$myurl.'&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect';
        header('Location:'.$wxurl);
    }

Process 2: get the code from the callback address. After getting the code, request an interface to get access_token and openid. After getting the openid, request the interface to get user information
	public function redirect_url(){
    	$appid="Access to WeChat public address"; //AppID
        $key="Access to WeChat public address"; //AppSecret
    	$code   = $_GET['code'];
        if ($code) {
           $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$key&code=$code&grant_type=authorization_code";
           $s = file_get_contents($url);
           $s = json_decode($s, true);
        }
        $access_token = $s["access_token"];
        $openid = $s['openid'];
        //Step 2: query user information according to global access_token and openid
        $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $userinfo = $this->http_curl($get_user_info_url); // Get user information
		print_r($user_info);
    }
	
	public function http_curl($url){
      //Pass parameters with curl
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

      //Turn off ssl authentication
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);


      curl_setopt($ch,CURLOPT_HEADER, 0);
      $output = curl_exec($ch);
      curl_close($ch);
      return json_decode($output, true);
    }

Last printed user information:

4, Pit summary:

Pits 1:

When requesting the first interface to obtain the code to pull up the authorization, an error is reported: the domain name of redirect \ u URI is inconsistent with the background configuration, and the error code is 10003! But there is no problem with the configuration of the public number, so when the first step requests the interface to get code, add the urlencode (callback address) to the callback address.

Pit two:

In the public number setting, only the authorized domain name can be configured. The domain name is not required to add http. And the callback address is set in the code itself, no need to configure it in the public address! !

Pit three:


If it is only for acquiring the authorized user information of h5 web page, the server configuration does not need to be started!!!

5, Wechat development documents

Links: Wechat access to webpage authorized development documents.

326 original articles published, 102 praised, 450000 visitors+
His message board follow

Tags: curl SSL

Posted on Sat, 18 Jan 2020 03:52:38 -0500 by nootkan