上一章写了如何创建自定义菜单,我们创建了登陆菜单,那么怎么让用户授权登录。
接口地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
接口细节就不记录了,直接就写授权的方法
$this->getUserInfo('/login/login');
这个函数就是获取用户的授权信息,函数里面是
需要复制的
public function getUserInfo($url)
{
$code = $_GET['code'];
if(!$code){
$this->get_authorize_url($url);
}else{
$op = $this->get_access_token($code);
if($op){
$userinfo = $this->get_user_info($op['access_token'],$op['openid']);
if($userinfo){
return $userinfo;
}
}
}
$this->respondError('请从微信中重新点击跳转!');
}
那么我们的基础微信的控制器直接粘贴过来里面有我们需要的函数:
namespace Home\Controller;
use Think\Controller;
/**
* 微信授权相关接口
*/
class WechatController extends Controller
{
public $app_id = '';
public $app_secret = '';
public $host = '';
/*构造方法*/
public function __construct()
{
parent::__construct();
$this->app_id = C('WECHAT_INIT')['appid'];
$this->app_secret = C('WECHAT_INIT')['appsecret'];
$this->host = C('WECHAT_HOST');
}
public function post_request($url, $post = null) {
$curl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
if ($post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {
return curl_error($curl);
}
curl_close($curl);
return $data;
}
/**
* 获取access_token
* @return mixed
*/
public function access_token()
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}";
$token_data = $this->post_request($url);
$token_data = json_decode($token_data, TRUE);
if($token_data['access_token'] != '')
{
return $token_data['access_token'];
}
}
/**
* 获取微信授权链接
* @param string $redirect_uri 跳转地址
* @param mixed $state 参数
*/
public function get_authorize_url($redirect_uri = '', $state = '')
{
$redirect_uri = urlencode($this->host.$redirect_uri);
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
header('Location: ' . $url);
die;
}
/**
* 获取授权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->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.weixin.qq.com/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_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);
}
}
获取到了用户的信息之后,就可以来做 用户注册,用户登录,的操作了 ,
每一个微信公众号的用户的微信openID都不一样,openID 是一个公众号的固定的,所以注册的时候可以存入用户的openID然后利用openID 可以给指定的用户推送消息等,做别的相对的操作。