【1】带微信帐号的手机
 
  
 
  
【2】打开浏览器,这里以IE为例。
 
  
输入:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
 
   
   
【3】用手机登录你的微信,使用微信中的“扫一扫”功能,扫描上面网页中的二维码。在手机上会出现以下界面:
 
    
   
【3】网页授权获取用户基本信息
 
    
    
   
注意:
 
   
这里的填写只要域名就可以里,不要http和以及域名下面的方法哦!
 
   
【4】然后在该域名下面定义一个控制器,添加方法即可
 
   
【5】方法一:跳转获取Code【需要微信登陆或者扫描的页面】
 
   
需要的参数:
 
   
(这里需要urlencode编码)
 
   
案例代码:
 
   
这里的:Scope为snsapi_base
 
   
/*
 *  定向的跳转,为了获取Code
 */
public function getcodeAction(){
    $this->view->disable();
$appid = 'wx94c43716d8a91f3f';
$redirect_uri = urlencode('http://ford4s.amailive.com/redis/getaccesstoken');
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1234#wechat_redirect";
    header('location:'.$url);
} 
   
【6】方法二:这个方法也就是第一个方法要跳转的回调函数,这个方法名就在方法一的URL地址中:
 
   
需要跳转的方法:
 
    
   
实例代码:
 
   
public function getaccesstokenAction(){
    $this->view->disable();
    $appid = 'wx94c43716d8a91f3f';
    $appsecret = 'd4624c36b6795d1d99dcf0547af5443d';
    /*回调的时候自带的这个参数*/
    $code = $_GET['code'];
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $data = curl_exec($ch);
    curl_close($ch);
    /*这里接收到的data数据是Json格式的,我在这转换成数组了*/
    $result = json_decode($data,true);
    /*取出数组中的access_token这个值*/
    $access_token = $result['access_token'];
    $expires_in = $result['expires_in'];
    /*拿到Openid就知道是哪个用户了,例如:参加活动次数,统计量的统计,没参加一下就写一次,在这里可以写入数据库*/
    $openid = $result['openid'];
    echo $openid;
} 
http://ford4s.amailive.com/redis/getcode【5】把域名和方法一生成一个二维码测试:
    
   
 
     
    
 
    
【6】返回信息:
 
    
{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" }
 
   
【7】获取用户信息:
 
   
(1)方法一:用户登陆或者扫描的方法
 
   
Scope为snsapi_userinfo
 
   
public function getcodeAction(){
    $this->view->disable();
$appid = 'wx94c43716d8a91f3f';
    /*基本授权 方法跳转地址*/
$redirect_uri = urlencode('http://ford4s.amailive.com/redis/getuserinfo');
    /*高级授权 snsapi_userinfo*/
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=1234#wechat_redirect";
    header('location:'.$url);
} 
   
(2)方法二:获取用户详细信息,【这个方法是在第一个跳转页面的时候加载域名后面的那个方法哦!】
 
   
public function getUserInfoAction(){
    $this->view->disable();
    $appid = 'wx94c43716d8a91f3f';
    $appsecret = 'd4624c36b6795d1d99dcf0547af5443d';
    /*回调的时候自带的这个参数*/
    $code = $_GET['code'];
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $data = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($data,true);
    /*取出数组中的access_token这个值*/
    $access_token = $result['access_token'];
    $openid = $result['openid'];
    $URL2 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $info = curl_exec($ch);
    curl_close($ch);
    var_dump($info);
} 
   
(3)打印结果:
 
    
   
(4)单独获取access_token方法:
 
   
参数说明:
 
   
公众号可以使用AppID和AppSecret调用本接口来获取access_token
 
   
案列代码:
 
   
/* AppID和AppSecret调用本接口来获取access_token */
public function getaccesstokenAction(){
    $this->view->disable();
    $appid = 'wx94c43716d8a91f3f';
    $appsecret = 'd4624c36b6795d1d99dcf0547af5443d';
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $data = curl_exec($ch);
    curl_close($ch);
    /*这里接收到的data数据是Json格式的,我在这转换成数组了*/
    $result = json_decode($data,true);
    /*取出数组中的access_token这个值*/
    $access_token = $result['access_token'];
    return $access_token;
} 
   
    
    
    
 
    
 
  
 
  
 
  
 
  
 
  
 
  
  
  
  
   
  
 
   
  










