大家好,又见面了,我是你们的朋友全栈君。
<?php
namespace app\common\wechat;
/**
* 微信开放平台
* Date: 2020/9/08 0015
* Time: 上午 10:03
*/
class WXOpenPlatform{
private static $appId = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
private static $appSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
private static $scope = 'snsapi_login';
/**
* curl请求
* @param $url
* @return mixed
*/
public static function https_request($url, $data = null) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/**
* 第一步:用户同意授权,获取code
* @param $redirect_uri
* @param int $state
* @return string
*/
public static function getCode($redirect_uri, $state = 1){
$redirect_uri = urlencode($redirect_uri); //用urlEncode对链接进行处理
$code_url = 'https://open.weixin.qq.com/connect/qrconnect?appid='.self::$appId.'&redirect_uri='.$redirect_uri.'&response_type=code&scope='.self::$scope.'&state='.$state.'#wechat_redirect';
return $code_url;
}
/**
* 第二步:通过code换取网页授权access_token
* 正常结果:return { "access_token":"ACCESS_TOKEN",
* "expires_in":7200,
* "refresh_token":"REFRESH_TOKEN",
* "openid":"OPENID",
* "scope":"SCOPE" }
* 错误是的结构:{"errcode":40029,"errmsg":"invalid code"}
*/
public static function getAccessToken($code){
//从微信接口获取
$accessTokenApi = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.self::$appId.'&secret='.self::$appSecret.'&code='.$code.'&grant_type=authorization_code';
$res = self::https_request($accessTokenApi);
$result = json_decode($res, true);
return $result;
}
/**
* 第三步:刷新access_token(如果需要)
* 正常结果:return { "access_token":"ACCESS_TOKEN",
* "expires_in":7200,
* "refresh_token":"REFRESH_TOKEN",
* "openid":"OPENID",
* "scope":"SCOPE" }
* 错误是返回结果:{"errcode":40029,"errmsg":"invalid code"}
*/
public static function refreshAccessToken($refreshToken){
$refreshAccessTokenApi = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.self::$appId.'&grant_type=refresh_token&refresh_token='.$refreshToken;
$res = self::https_request($refreshAccessTokenApi);
$result = json_decode($res, true);
return $result;
}
/**
* 第四步:拉取用户信息
* 正常返回结果:{"openid":" OPENID",
* "nickname": NICKNAME,
* "sex":"1",
* "province":"PROVINCE"
* "city":"CITY",
* "country":"COUNTRY",
* "headimgurl":"http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ
* 4eMsv84eavHiaiceqxibJxCfHe/46",
* "privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
* "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
* }
* 错误时返回结果:{"errcode":40003,"errmsg":" invalid openid "}
*/
public static function getUserInfo($openId,$accessToken){
$userInfoApi = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$accessToken.'&openid='.$openId.'&lang=zh_CN';
$res = self::https_request($userInfoApi);
$result = json_decode($res, true);
return $result;
}
/**
* 附:检验授权凭证(access_token)是否有效
* 正确的JSON返回结果:
* { "errcode":0,"errmsg":"ok"}
* 错误时的JSON返回示例:
* { "errcode":40003,"errmsg":"invalid openid"}
*/
public static function checkAccessToken($openId,$accessToken){
$checkAccessTokenUrl = 'https://api.weixin.qq.com/sns/auth?access_token='.$accessToken.'&openid='.$openId;
$res = self::https_request($checkAccessTokenUrl);
$result = json_decode($res, true);
return $result;
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/141030.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...