微信-OAuth2.0鑒權

OAuth2.0鑒權

  • 公眾號可以通過微信網頁授權機制,來獲取用戶基本信息,進而實現業務邏輯。
  • 網頁授權獲取用戶基本信息
    點擊修改設置授權回調域名
在微信公眾號請求用戶網頁授權之前,開發者需要先到公眾平臺官網
中的開發者中心頁配置授權回調域名。請注意,這里填寫的是域名
(是一個字符串),而不是URL,因此請勿加[http://等協議頭;]
(http://xn--%3B-nv8a14pdn5bver/)2、授權回調域名配置規范為
全域名,比如需要網頁授權的域名為:www.qq.com,配置以后此域
名下面的頁面[http://www.qq.com/music.html]
(http://www.qq.com/music.html) 、 
[http://www.qq.com/login.html]
(http://www.qq.com/login.html) 都可以進行OAuth2.0鑒權。但
[http://pay.qq.com](http://pay.qq.com/) 、 
[http://music.qq.com](http://music.qq.com/) 、 
[http://qq.com無法進行OAuth2.0鑒權](http://qq.xn--
comoauth2-735sh62dwk9eysua.xn--0-k76bu98j/)3、如果公眾
號登錄授權給了第三方開發者來進行管理,則不必做任何設置,由第
三方代替公眾號實現網頁授權即可

<?php
/*
本文件位置
*/
$redirect_url= "http://www.camera.0fees.us/index.php";
/*
URL
*/
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6292681b13329528&redirect_uri=http://israel.duapp.com/weixin/oauth2_openid.php&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

$code = $_GET["code"];
$userinfo = getUserInfo($code);

function getUserInfo($code)
{
  $appid = "wxb202efbc9c76046a";
  $appsecret = "b8d396c379cc2b2277c4f1c062365f3a";

    //oauth2的方式獲得openid
  $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
  $access_token_json = https_request($access_token_url);
  $access_token_array = json_decode($access_token_json, true);
  $openid = $access_token_array['openid'];

    //非oauth2的方式獲得全局access token
    $new_access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
    $new_access_token_json = https_request($new_access_token_url);
  $new_access_token_array = json_decode($new_access_token_json, true);
  $new_access_token = $new_access_token_array['access_token'];
    
    //全局access token獲得用戶基本信息
    $userinfo_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$new_access_token&openid=$openid";
  $userinfo_json = https_request($userinfo_url);
  $userinfo_array = json_decode($userinfo_json, true);
  return $userinfo_array;
}

function https_request($url)
{
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $data = curl_exec($curl);
  if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
  curl_close($curl);
  return $data;
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD><TITLE>OAuth2.0認證</TITLE>
    <META charset=utf-8>
    <META name=viewport content="width=device-width, user-scalable=no, initial-scale=1">
    <LINK rel="stylesheet" href="css/jquery.mobile-1.3.2.css">
    <SCRIPT src="js/jquery-1.10.2.js"></SCRIPT>
    <SCRIPT src="js/jquery.mobile-1.3.2.js"></SCRIPT>
  </HEAD>

  <BODY>
    <script type="text/javascript">
      document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
        WeixinJSBridge.call('hideOptionMenu');
      });
      document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
        WeixinJSBridge.call('hideToolbar');
      });
    </script>
    <div data-role="page" id="page1">
      <div data-role="content">
        <div style=" text-align:center">
          <img style="width: 99%; height: %" src="<?php echo $userinfo["headimgurl"];?>">
        </div>
        <UL data-role="listview" data-inset="true">
          <LI>
            <P>
              <div class="fieldcontain">
                <label for="subscribe">是否關注</label>
                <input name="subscribe" id="subscribe" value="<?php echo $userinfo["subscribe"];?>" type="text" >
              </div>
              <div class="fieldcontain">
                <label for="openid">OpenID</label>
                <input name="openid" id="openid" value="<?php echo $userinfo["openid"];?>" type="text" >
              </div>
              <div class="fieldcontain">
                <label for="nickname">昵稱</label>
                <input name="nickname" id="nickname" value="<?php echo $userinfo["nickname"];?>" type="text" >
              </div>
              <div class="fieldcontain">
                <label for="sex">性別</label>
                <input name="sex" id="sex" value="<?php echo $userinfo["sex"];?>" type="text" >
              </div>
              <div class="fieldcontain">
                <label for="country">國家</label>
                <input name="country" id="country" value="<?php echo $userinfo["country"];?>" type="text" >
              </div>
              <div class="fieldcontain">
                <label for="province">省份</label>
                <input name="province" id="province" value="<?php echo $userinfo["province"];?>" type="text" >
              </div>
              <div class="fieldcontain">
                <label for="city">城市</label>
                <input name="city" id="city" value="<?php echo $userinfo["city"];?>" type="text" >
              </div>
              <div class="fieldcontain">
                <label for="subscribe_time">關注時間</label>
                <input name="subscribe_time" id="subscribe_time" value="<?php echo $userinfo["subscribe_time"];?>" type="text" >
              </div>
            </P>
          </LI>
        </UL>
      </div>
      <div data-theme="a" data-role="footer" data-position="fixed">
        <h3>方倍工作室</h3>
      </div>
    </div>

  </BODY>
</HTML>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容