首頁 > 後端開發 > php教程 > 微信公眾平台OAuth20網頁授權php範例

微信公眾平台OAuth20網頁授權php範例

WBOY
發布: 2016-07-28 08:27:40
原創
1421 人瀏覽過

1、設定授權回呼頁域名,如 www.aaa.com

2、模擬公眾號的第三方網頁,fn_system.php

Php代碼   微信公众平台OAuth20网页授权php示例

  1. if(emptyempty(
  2. empty(
  3. empty(empty [
  4. 'user'])){  
  5.   header(
  6. "Location:http://www.aaa.com/uc/fn_wx_login.php");  }
  7. }
  8. }
  9. }else
  10. {  
    print_r($_SESSION

[

'user'  微信公众平台OAuth20网页授权php示例]);  

  1. }  
  2.  >
  3. 3、訪問第三方網頁時,如果檢查session中不存在會話信息,則跳轉至登陸頁,fn_wx_login.php
  4. Php代碼  
  5.     $appid = "微信在微信的appid"
  6.     $url
  7.  = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='
.

$appid

.

'&redirect_uri=http%3a%2f%2fwww.aaa.com%2fuc%2ffn_callback.php&resp/s>

    header( 微信公众平台OAuth20网页授权php示例"Location:"

.$url.$url.$url.$url.$url.$url.$url.$url。  ?>  5、在微信的使用者授權頁,如果使用者選擇了“同意授權”,則當微信重新回跳至第三方網頁的回跳位址時,會附帶code參數。 6、第三方網頁的回跳url中,先從請求中取得code,然後根據code進一步換取openid和access_token,然後就可以根據openid和access_token調用微信的相關介面查詢用戶資訊了。 Php代碼  
  1.   
  2.  = "大眾號在微信的appid";  d = 
  3. "公眾號在微信的app secret";  
  4. <span>$_GET<span></span>[<span></span>"code"<span></span>];  <span></span><span></span><span></span>$get_token_url</span> = <li> <span>'https://api.weixin.qq.com/sns/oauth2/access_token?appid='<span></span>.<span>.</span>$appid<span></span>.<span></span>'&secret='<span></span>.<span></span>$secret<span></span>.<span></span>$secret<span></span>.<span></span></span>.<span></span>$code<span></span>.<span></span>'&grant_type=authorization_code'<span></span>; ><span></span>  </li> <li> <span></span>$ch</li> <li> = curl_init(); curl_setopt(<span>$ch<span></span>,CURLOPT_URL,<span></span>$get_token_url</span> </li>);  <li> <span><span>$ch</span><span>,CURLOPT_HEADER,0);  </span><span></span><span></span>curl_setopt(</span>$ch</li> <li>curl_setopt(<span>$ch<span></span>curl_setopt(<span>$ch</span></span>curl_setopt(</li>$ch<li>$ch<span></span><span></span>curl_setopt($ch</li> <li>, CURLOPT_CONNECTTIMEOUT, 10);  <span><span>$res</span><span> = curl_exec(</span></span>$ch</li> <li>);  <span><span></span><span></span> ch<span></span>);  <span></span></span> </li> <li><span>$json_obj<span></span> > 🎜>,true);  <span></span></span></li> <li>  <span><span></span><span></span>///openpid和>  <span></span><span></span></span> </li>$access_token<li><span> = </span></li>$json_obj<span>];  </span><span></span><li>$openid<span><span></span><span>$openid</span><span></span><span>$openid</span><span>[</span><span>'openid'</span></span>];  </li> <li> <span><span></span><span>$get_user_info_url</span><span></span>$get_user_info_url<span></span><span>$get_user_info 🎜>'https://api.weixin.qq.com/sns/userinfo?access_token='</span><span>.</span></span>$access_token</li> <li>.<span><span></span><span>.</span><span></span> '&openid='<span></span>.<span></span>$openid<span></span>.<span></span>'&lang=zh_CN'<span></span><span>'&lang=zh_CN'</span><span> </span><span></span>  <span></span></span> </li> <li>$ch<span></span> = curl_init(); 🎜></li> = curl_init(); 🎜>curl_setopt(<li>$ch<span><span>,CURLOPT_URL,</span><span>$get_user_info_url</span></span>); (</li>$ch<li> <span>,CURLOPT_HEADER,0);  <span></span><span></span><span>curl_setopt(</span>$ch<span></span>curl_setopt(</span>$ch</li> <li>curl_setopt(<span>$ch</span><span></span>curl_setopt(</li>$ch<li><span>, CURLOPT_CONNECTTIMEOUT, 10);<span>, CURLOPT_CONNECTTIMEOUT, 10); ></span>$res<span></span> = curl_exec(</span></li>$ch<li> <span>);  <span></span>);  </span><url>$ch</url> </li> <li>);  <span><span></span><span></span>  <span></span><span></span></span> </li> <li>  <span><span></span><span></span></span>$user_obj</li> <li> = json_decode(<span>$res</span>$res</li> <li><span><span></span>$_SESSION<span></span>[</span></li>'user'<li> <span>]>_objo<span>'user'</span><span>]>_objo</span></span>;  <span></span> </li> <li>print_r(<span>$user_obj<span></span>);; 🎜>  <span></span><span></span>?>  <span></span><span></span><span> </span> 以上就介紹了 微信公眾平台OAuth20網頁授權php範例,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。 </span> </li> <li>
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板