PHP 微信客户端 网页授权登录

  1. <?php
  2. namespace Apps\Controller;
  3. use Common\Controller\AppBaseController;
  4. define("TOKEN", "");//你微信定义的token
  5. define("APPID", "");//你微信定义的appid
  6. define("APPSECRET","");//你微信公众号的appsecret
  7.             session_start;//打开session
  8. class TestController extends AppBaseController //继承的这个控制器没用 你可以继承其他的任意一个
  9. {
  10.     //第一步:用户同意授权,获取code
  11.     function accept(){
  12.         //这个链接是获取code的链接 链接会带上code参数
  13.         $REDIRECT_URI = "http://www.icoco.xin/wxt_webhome/index.php/Apps/Test/getCode";
  14.         echo $REDIRECT_URI."<br>";
  15.         $REDIRECT_URI = urlencode($REDIRECT_URI);
  16.         echo $REDIRECT_URI."<br>";
  17.         $scope = "snsapi_userinfo";
  18.         echo $scope."<br>";
  19.         $state = md5(mktime());
  20.         echo $state."<br>";
  21.         $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".$REDIRECT_URI."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
  22.         header("location:$url");
  23.         
  24.     }
  25.     //用户同意之后就获取code  通过获取code可以获取一切东西了  机智如我
  26.     function getCode(){
  27.         //获取accse_token
  28.         $code = $_GET["code"];
  29.         //echo $code;
  30.                 //echo "<br>";
  31.         //用code获取access_yoken
  32.         $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$code."&grant_type=authorization_code";
  33.         //这里可以获取全部的东西  access_token openid scope
  34.         $res = $this->https_request($url);
  35.         $res  = json_decode($res,true);
  36.         $openid = $res["openid"];
  37.         echo "<pre>";
  38.         //print_r($res);
  39.         //echo $openid;
  40.         //echo "<br>";
  41.         $access_token = $res["access_token"];
  42.         //echo $access_token;
  43.         //这里是获取用户信息
  44.         $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
  45.         $res = $this->https_request($url);
  46.         $res = json_decode($res,true);
  47.         //写入session
  48.         print_r($res);
  49.         //把用户的信息写入session 以备查用
  50.         $weixn = $res["openid"];
  51.         $nickname = $res["nickname"];
  52.             $_SESSION["weixin"]=$weixin;
  53.          header("location:http://www.icoco.xin/wxt_webhome/test.php");
  54.     }
  55.     function https_request($url, $data = null)
  56. {
  57.     $curl = curl_init();
  58.     curl_setopt($curl, CURLOPT_URL, $url);
  59.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  60.     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  61.     if (!empty($data)){
  62.         curl_setopt($curl, CURLOPT_POST, 1);
  63.         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  64.     }
  65.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  66.     $output = curl_exec($curl);
  67.     curl_close($curl);
  68.     return $output;
  69. }
  70. }//classend

猜你喜欢

转载自blog.csdn.net/u013239233/article/details/73161959