php 网站微信第三方登陆

微信的类文件

我写在weichat.php中

<?php

namespace app\common;
class Wechat {

//高级功能-》开发者模式-》获取
private $app_id = 'xxxxxxxxxxxxxxxxxxxx'; //公众号appid
private $app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; //公众号app_secret
private $redirect_uri = 'http://www.xxxx.com/dovxlogin'; //授权之后跳转地址
/**
* 获取微信授权链接
*
* @param string $redirect_uri 跳转地址
* @param mixed $state 参数
*/
public function get_authorize_url($state)
{
$redirect_uri = urlencode($this->redirect_uri);
return "https://open.weixin.qq.com/connect/qrconnect?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
}
/**
* 获取授权token
*
* @param string $code 通过get_authorize_url获取到的code
*/
public function get_access_token($code)
{
$token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code";
$token_data = $this->https_request($token_url);
//return $token_data;

return json_decode($token_data, TRUE);



}

/**
* 获取授权后的微信用户信息
*
* @param string $access_token
* @param string $open_id
*/
public function get_user_info($access_token,$open_id)
{
	if ($access_token && $open_id) {
		$info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN";
		$info_data = $this->https_request($info_url);


		return json_decode($info_data, TRUE);

	}
}

	//https请求
	public 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;
	}

}
?>

controller中的方法

/**
	 * 第三方  微信登录
	 */
	public function VxLogin()
	{
		$weixin = new Wechat();
		$url = $weixin->get_authorize_url('xxxxxx');
		header("Location: ".$url."");
		die;

	}

	/**
	 * 第三方  微信登录
	 */
	public function doVxLogin()
	{
		$weixin = new Wechat();
		if (isset($_GET['code']))
		{
			$data = $weixin->get_access_token($_GET['code']);
			//获取个人信息
			$user = $weixin->get_user_info($data["access_token"],$data["openid"]);

//		//	已经拿到各种信息 ,这里写你需要处理的方法

			}else{
				//没有openID的情况
//				
			}


		}else{
			echo "NO CODE";
		}

	}


要使用PHP完成微信第三方平台授权,你可以按照以下步骤进行操作: 1. 首先,你需要注册成为微信开放平台的开发者,并创建一个第三方平台应用。 2. 在创建应用后,你会得到一个AppID和AppSecret,这些是与微信平台进行通信的凭证。 3. 在你的PHP项目中,你需要使用curl库或其他HTTP请求库来发送HTTP请求到微信开放平台的API接口。 4. 首先,你需要获取预授权码(pre_auth_code)。发送GET请求到以下接口: ``` https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=xxx ``` 其中,component_access_token是通过调用获取第三方平台component_access_token接口获取的。 5. 获取到预授权码后,你可以使用预授权码和你的AppID生成授权链接,引导用户进入授权页面: ``` https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxx&pre_auth_code=xxx&redirect_uri=xxx ``` 其中,component_appid是你的AppID,pre_auth_code是上一步获取的预授权码,redirect_uri是用户授权后的回调URL。 6. 用户在授权页面确认授权后,会跳转到你指定的回调URL,并携带授权码(authorization_code)参数。 7. 在回调URL对应的PHP页面中,你需要解析URL中的授权码参数,并使用授权码发送POST请求到以下接口,获取授权令牌(authorizer_access_token)和刷新令牌(authorizer_refresh_token): ``` https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=xxx ``` 其中,component_access_token是通过调用获取第三方平台component_access_token接口获取的。 8. 获取到授权令牌和刷新令牌后,你可以使用它们来调用微信开放平台的其他API接口,完成后续的操作。 以上是使用PHP完成微信第三方平台授权的基本流程,具体的实现细节可能会因具体需求而有所不同。在实际开发中,你还需要处理授权过期、刷新令牌等情况,以保证授权的有效性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值