100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > php公众获取用户信息 PHP--通过公众号获取用户微信信息

php公众获取用户信息 PHP--通过公众号获取用户微信信息

时间:2021-04-19 01:21:41

相关推荐

php公众获取用户信息 PHP--通过公众号获取用户微信信息

/**

*This is test. 这是一个测试

*/ function bbc(){

session("name","你打野");

redirect(U("Home/Index/index_1"));

}

function index_1() {

$code = I("code");

$userinfo = $this->get_access_token($code);

//用户授权

if ($userinfo) {

$openid = $userinfo['openid'];

$access_token = $userinfo['access_token'];

$user = $this->get_user_info($access_token, $openid);

$mod = M("member");

$where['open_id'] = $openid;

//判断用户已注册

$res = $mod->where($where)->find();

//数据库无匹配

if (!$res) {

//注册新用户

$add = array(

"open_id" => $openid,

"nickname" => $user['nickname'],

"username" => $user['nickname'],

"city" => $user['city'],

"province" => $user['province'],

"country" => $user['country'],

"pic" => $user['headimgurl'],

"sex" => $user['sex'],

"reg_time" => time(),

"open_type"=>1,

);

$mod->add($add);

$res = $mod->where($where)->find();

}

// exit("test");

// $_COOKIE['user'] = $res; //保存用户信息到会话中

// $_SESSION['user'] = $res; //保存用户信息到会话中

$_SESSION['user']=$res;

//修改最后登录时间

// $mod->where($where)->setField("end_time",time());

//跳转网页

$url="http://".$_SERVER['HTTP_HOST']."";

redirect($url);

} else {

echo '';

echo "未授权无法访问---(You are not eligible to visit)";

}

}

/**

* 获取微信授权链接

*

* @param string $redirect_uri 跳转地址

* @param mixed $state 参数

*/

public function get_authorize_url($redirect_uri = '', $state = '') {

$redirect_uri = urlencode($redirect_uri);

return "https://open./connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";

}

/**

* 获取授权token

*

* @param string $code 通过get_authorize_url获取到的code

*/

public function get_access_token($code = '', $app_id = '', $app_secret = '') {

$app_id = empty($app_id) ? $this->app_id : $app_id;

$app_secret = empty($app_secret) ? $this->app_secret : $app_secret;

$token_url = "https://api./sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code";

$token_data = $this->http($token_url);

if ($token_data[0] == 200) {

return json_decode($token_data[1], TRUE);

}

return FALSE;

}

/**

* 获取授权后的微信用户信息

*

* @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./sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN";

$info_data = $this->http($info_url);

if ($info_data[0] == 200) {

return json_decode($info_data[1], TRUE);

}

}

return FALSE;

}

public function http($url, $method, $postfields = null, $headers = array(), $debug = false) {

$ci = curl_init();

/* Curl settings */

curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);

curl_setopt($ci, CURLOPT_TIMEOUT, 30);

curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);

switch ($method) {

case 'POST':

curl_setopt($ci, CURLOPT_POST, true);

if (!empty($postfields)) {

curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);

$this->postdata = $postfields;

}

break;

}

curl_setopt($ci, CURLOPT_URL, $url);

curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ci, CURLINFO_HEADER_OUT, true);

$response = curl_exec($ci);

$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);

if ($debug) {

echo "=====post data======\r\n";

var_dump($postfields);

echo '=====info=====' . "\r\n";

print_r(curl_getinfo($ci));

echo '=====$response=====' . "\r\n";

print_r($response);

}

curl_close($ci);

return array($http_code, $response);

}

}

ps:封装成了一个类,不记得是转自哪里了。不明白的地方可以让我和你一起探讨,Q:1411479499

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。