歡迎來到安信科技官方網站!【www.boomerlogic.com】
      18112005550
      工作時間: 8:30-21:30
      新聞中心
      News Center

      thinhkphp微信網頁授權登錄源碼

      資訊分類: 移動微信  瀏覽: 2020年7月29日
      參考文檔:https://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html


      代碼
      <?php
      namespace Org\WeChat;
      /**
      * 微信授權相關接口
      *
      * @link http://www.boomerlogic.com
      */
      class Wechat {
      //高級功能-》開發者模式-》獲取
      private $app_id = ''; //公眾號appid
      private $app_secret = ''; //公眾號app_secret
      private $redirect_uri = ''; //授權之后跳轉地址
      /**
      * 獲取微信授權鏈接
      *
      * @param string $redirect_uri 跳轉地址
      * @param mixed $state 參數
      */
      public function get_authorize_url($state)
      {
      $redirect_uri = urlencode($this->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($code)
      {
      $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,$open_id)
      {
      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_SSL_VERIFYPEER, FALSE);
      curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);
      curl_setopt($ci, CURLOPT_TIMEOUT, 30);
      curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
      switch ($method) {
      case 'POST':
      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 '=====info=====' . "\r\n";
      print_r(curl_getinfo($ci));
      echo '=====$response=====' . "\r\n";
      print_r($response);
      }
      curl_close($ci);
      return array($http_code, $response);
      }
      }

      Copyright © 2007-2024 安信科技(十五周年紀念版) All Rights Reserved  備案號:蘇ICP備15047094號-3 
      網站首頁 |  新聞資訊 |  服務項目 |  軟件產品 |  試用下載 |  需求提交 |  模版建站 |  關于安信 |  產品授權 |  聯系我們 |  定制開發 | 
      服務熱線:181-1200-5550  客服QQ: 120094883  | 郵箱:120094883#qq.com(#改@)