Home >Backend Development >PHP Problem >How to implement WeChat login in php

How to implement WeChat login in php

王林
王林Original
2021-10-14 15:21:295593browse

php method to implement WeChat login: 1. Obtain code with user consent and authorization; 2. Exchange code for web page authorization access_token; 3. Obtain user information.

How to implement WeChat login in php

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

It is not difficult to use PHP to log in to WeChat. It can be simply divided into three steps, as shown below:

Step one: The user agrees to authorize and obtain the code

//微信登录
	public function wxlogin()
	{
		$appid = "";
   	 	$secret = "";
   	 	
   	 	$str="http://***.***.com/getToken";
   	 	$redirect_uri=urlencode($str);
		//通过code获得 access_token + openid
		$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
      
       header("Location:" . $url);
	}

Step 2: Exchange webpage authorization access_token through code

public function getToken()
{
$code = $_GET["code"];

        $appid = "";
       $secret = "";

//通过code获得 access_token + openid
       $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid
        ."&secret=" . $secret . "&code=" . $code . "&grant_type=authorization_code";
        $jsonResult =$this->https_request($url);
        $resultArray = json_decode($jsonResult, true);

        $access_token = $resultArray["access_token"];
        $openid = $resultArray["openid"];

//第三步  获取用户信息
         //通过access_token + openid 获得用户所有信息,结果全部存储在$infoArray里,后面再写自己的代码逻辑
        $infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid.'&lang=zh_CN';
        $infoResult = $this->https_request($infoUrl);
        $infoArray = json_decode($infoResult, true);

       if($infoArray['unionid'])
        {
        
     }

}

Additional: Methods used in the code

// An highlighted block
function https_request($url, $data = null)
	{
	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	    if (!empty($data)){
	        curl_setopt($curl, CURLOPT_POST, 1);
	        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	    }
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	    $output = curl_exec($curl);
	    curl_close($curl);
	    return $output;
	}

Recommended learning: php training

The above is the detailed content of How to implement WeChat login in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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