Rumah  >  Artikel  >  applet WeChat  >  使用php微信开发获取用户信息实现代码详解

使用php微信开发获取用户信息实现代码详解

高洛峰
高洛峰asal
2017-03-20 14:33:242443semak imbas

这篇文章主要介绍了php 微信开发获取用户信息如何实现的相关资料,需要的朋友可以参考下

php 微信开发获取用户信息

获取用户信息的大致算法是

用户授权登录第三方网站,

重点:scope参数:
snsapi_basic 静默登录,不需要用户授权,只能获取到openid;
snsapi_userinfo ,需要用户点击授权,能获取到openid和所有用户信息;

第一步:先获取用户的code值;
第二步:根据code值去获取access_token,每次请求的值都不一样,如果没有使用,每五分钟更新一次;
第三步:根据access_token获取用户信息;

1.获取code代码实现:

使用php微信开发获取用户信息实现代码详解

使用php微信开发获取用户信息实现代码详解

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


Atas ialah kandungan terperinci 使用php微信开发获取用户信息实现代码详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn