Home > Backend Development > PHP Tutorial > Detailed explanation of PHP WeChat web page authorization development

Detailed explanation of PHP WeChat web page authorization development

墨辰丷
Release: 2023-03-29 19:36:01
Original
2163 people have browsed it

This article mainly introduces the detailed explanation of PHP WeChat web page authorization development. Interested friends can refer to it. I hope it will be helpful to everyone.

WeChat web page authorization is an advanced function only available for service accounts. Developers can obtain basic information of users through authorization. Before this, the only way to obtain message information was through openid when the user interacted with the official account. Obtain user information; WeChat web page authorization can obtain the user's basic information without requiring message interaction or attention.

WeChat web page authorization is completed through OAuth2.0. The whole process is divided into three steps:

  • User authorization, obtain code;

  • Get access_token based on code [can be refreshed through refresh_token to obtain a longer validity period]

  • Get user information through access_token and openid

A simple encapsulation of the WeChat webpage authorization process:

 <?php
 
/**
 * 微信授权相关接口
 */
 
class Wechat {
  
  //高级功能-》开发者模式-》获取
  private $app_id = &#39;xxx&#39;;
  private $app_secret = &#39;xxxxxxx&#39;;
 
 
  /**
   * 获取微信授权链接
   * 
   * @param string $redirect_uri 跳转地址
   * @param mixed $state 参数
   */
  public function get_authorize_url($redirect_uri = &#39;&#39;, $state = &#39;&#39;)
  {
    $redirect_uri = urlencode($redirect_uri);
    return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
  }
  
  /**
   * 获取授权token
   * 
   * @param string $code 通过get_authorize_url获取到的code
   */
  public function get_access_token($app_id = &#39;&#39;, $app_secret = &#39;&#39;, $code = &#39;&#39;)
  {
    $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code";
    $token_data = $this->http($token_url);
    
    if($token_data[0] == 200)
    {
      return json_decode($token_data[1], TRUE);
    }
    
    return FALSE;
  }
  
  /**
   * 获取授权后的微信用户信息
   * 
   * @param string $access_token
   * @param string $open_id
   */
  public function get_user_info($access_token = &#39;&#39;, $open_id = &#39;&#39;)
  {
    if($access_token && $open_id)
    {
      $info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN";
      $info_data = $this->http($info_url);
      
      if($info_data[0] == 200)
      {
        return json_decode($info_data[1], TRUE);
      }
    }
    
    return FALSE;
  }
  
  public function http($url, $method, $postfields = null, $headers = array(), $debug = false)
  {
    $ci = curl_init();
    /* Curl settings */
    curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ci, CURLOPT_TIMEOUT, 30);
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
 
    switch ($method) {
      case &#39;POST&#39;:
        curl_setopt($ci, CURLOPT_POST, true);
        if (!empty($postfields)) {
          curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
          $this->postdata = $postfields;
        }
        break;
    }
    curl_setopt($ci, CURLOPT_URL, $url);
    curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ci, CURLINFO_HEADER_OUT, true);
 
    $response = curl_exec($ci);
    $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
 
    if ($debug) {
      echo "=====post data======\r\n";
      var_dump($postfields);
 
      echo &#39;=====info=====&#39; . "\r\n";
      print_r(curl_getinfo($ci));
 
      echo &#39;=====$response=====&#39; . "\r\n";
      print_r($response);
    }
    curl_close($ci);
    return array($http_code, $response);
  }
 
}
Copy after login

##Summary: That’s it. The entire content of the article is hoped to be helpful to everyone's study.

Related recommendations:

Detailed explanation of conversion between arrays and XML processed by PHP

php for loop method and examples detailed explanation

##Detailed explanation of methods and examples of php file operation

##

The above is the detailed content of Detailed explanation of PHP WeChat web page authorization development. For more information, please follow other related articles on the PHP Chinese website!

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