首頁  >  文章  >  後端開發  >  PHP微信網頁授權開發詳解

PHP微信網頁授權開發詳解

墨辰丷
墨辰丷原創
2018-06-05 14:00:402089瀏覽

這篇文章主要介紹PHP微信網頁授權開發詳解,有興趣的朋友參考下,希望對大家有幫助。

微信網頁授權是服務號碼才有的高級功能,開發者可以透過授權後獲取使用者的基本資訊;在此之前,想要取得訊息資訊只能在使用者和公眾號互動時根據openid取得使用者資訊;而微信網頁授權可在不需要訊息交互,也不需要關注的情況下取得使用者的基本資訊。

微信網頁授權時透過OAuth2.0完成的,整個過程分為三個步驟:

  • 使用者授權,取得code;

  • #根據code取得access_token【可透過refresh_token刷新取得較長有效期限】

  • 透過access_token和openid取得使用者資訊

對微信網頁授權過程做了簡單封裝:

 <?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);
  }
 
}

#總結#:以上就是本篇文的全部內容,希望對大家的學習有所幫助。

相關推薦:

PHP處理陣列與XML之間的互相轉換詳解

php for 迴圈所使用的方法及實例詳解

php檔案操作的方法及實例詳解

以上是PHP微信網頁授權開發詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn