首頁  >  文章  >  微信小程式  >  php 實作微信開發獲取使用者訊息

php 實作微信開發獲取使用者訊息

墨辰丷
墨辰丷原創
2018-05-29 15:48:272867瀏覽

這篇文章主要介紹了php 微信開發獲取用戶資訊如何實現的相關資料,需要的朋友可以參考下

php 微信開發獲取用戶資訊

取得使用者資訊的大致演算法是

使用者授權登入第三方網站,

# 重點:scope參數:
snsapi_basic 靜默登錄,不需要使用者授權,只能取得到openid;
snsapi_userinfo ,需要使用者點擊授權,能取得到openid和所有使用者資訊;

第一步:先取得使用者的code值;
第二步:根據code值去取得access_token,每次請求的值都不一樣,如果沒有使用,每五分鐘更新一次;
第三個步驟:根據access_token取得使用者資訊;

1.取得code代碼實現:

getcode.php


if(isset($_SESSION['user'])){
              print_r($_SESSION['user']);
              exit;
            }

$appid='wx1d7c6fcd6131143b3';

            $redirect_url="http://www.antfortune.vip/callback.php";
            $scope='snsapi_userinfo';//获取的方式;


            $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlencode($redirect_url).'&response_type=code&scope='.$scope.'&state=123#wechat_redirect';


header("Location:".$url);


2、根據code取得access_token和openid


getOpenid.php



<?php
//获取用户openid
$appid="your appid";
$appsecret="your appsecret";
$code=$_GET[&#39;code&#39;];



function getOpenID($appid,$appsecret,$code){
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=". 
$appsecret."&code=".$code."&grant_type=authorization_code";

$weixin=file_get_contents($url);//通过code换取网页授权access_token
$jsondecode=json_decode($weixin); //对JSON格式的字符串进行编码
$array = get_object_vars($jsondecode);//转换成数组
$openid = $array[&#39;openid&#39;];//输出openid
return $openid;
}

echo getOpenID($appid,$appsecret,$code);


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


相關推薦:

【微信開發】微信支付參數設定【圖】,信開發支付參數設定

微信支付開發流程,信支付開發流程

#微信開發程式碼

####################### #######

以上是php 實作微信開發獲取使用者訊息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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