跳转到帖子
登录关注  
墨香年少

php获取公众号openid

已推荐帖子

<?php

$appid  = 'wx111112222222233333';
$secret = 'qwertyuiasdfghj23456789fcdgvhbjnk';
$callback = urlencode('https://wx.abcdef.com/get_openid/request.php');

if(!isset($_GET['code']))
{
	$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$callback."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
	go($url);
}
else
{
	$code = $_GET['code'];
	$api = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
	$ret = http($api);
	$arr = json_decode($ret,true);
	echo '<meta content="width=device-width, initial-scale=1" name="viewport" />';
	print_r($arr['openid']);die();
}



function go($url)
{
	header('location:'.$url);
	exit();
}
function http($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;
}

 


目之所及,皆是回忆,心之所想,皆是过往

分享这篇帖子


链接帖子
分享到其他站点

创建帐户或登录来提出意见

你需要成为会员才能提出意见

创建帐户

注册成为会员。只要几个简单步骤!

注册帐户

登录

已有帐户? 请登录。

现在登录
登录关注  

×
×
  • 创建新的...

重要信息

注册必须使用2-8个中文汉字作为账号