tp5微信网页授权接口开发,获取微信用户信息

use think\Controller;
use think\Db;
class Hd extends Controller
{
    private $appid  = "wx47d06f01aca201f5";   //你的appId
    private $secret = "50263c6adbbe9f23a2cb7d721de28985";   //你的appSecret

    //获取用户的openid
    function index(){
        //1.获取到code=================================
        $redirect_uri = urlencode("http://".$_SERVER['HTTP_HOST']."/index/hd/getUserInfo");//这里的地址需要http://
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
        $this->redirect($url);
        exit();
    }
    //通过post获取用户的信息
    function getJson($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }
    //获取用户的openid
    function getUserInfo(){
        $data = request()->get();
        $code   = $data['code'];
        //2.获取到网页授权的access_token===============
        //第一步,获取access_token
        //新建一个文件存储access_token,避免多次获取,access_token的有效时间是2个小时,这里的目录是根目录:www.xxx.xxx/access_token.txt
        $file_name = 'access_token.txt';
        if (!file_exists($file_name)) {     //文件不存在,新建并附赋权限
            $my_file = fopen($file_name, "w");
            $file_name = $my_file;
            chmod($file_name,0777);
        }
        $content = file_get_contents($file_name);  //读取文件内容
        $arr = json_decode($content,true);  //转化为数组
        if (is_array($arr)) {
            if (isset($arr['end_time']) && $arr['end_time'] > time()) {  //access_token未过时,直接拿来使用
                $token = $arr;
            } else {    //access_token超时,重新获取
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->secret";
                $token = $this->getJson($url);
                $token['start_time'] = time();
                $token['end_time']   = time()+7000;
                $my_file = fopen($file_name, "w") or die("Unable to open file!");
                $txt = json_encode($token);
                fwrite($my_file, $txt);
                fclose($my_file);
            }
        } else {     //用于新建文件时的写入
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->secret";
            $token = $this->getJson($url);
            $token['start_time'] = time();
            $token['end_time']   = time()+7000;
            $my_file = fopen($file_name, "w") or die("Unable to open file!");
            $txt = json_encode($token);
            fwrite($my_file, $txt);
            fclose($my_file);
        }
        //第二步:取得openid
        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appid&secret=$this->secret&code=$code&grant_type=authorization_code";
        $oauth2 = $this->getJson($oauth2Url);
        $openid = $oauth2['openid'];
        //第三步:根据全局access_token和openid查询用户信息
        $access_token = $token["access_token"];
        $openid = $oauth2['openid'];
        $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
        $user_info = $this->getJson($get_user_info_url);
        $user_info=json_encode($user_info);
        $this->redirect('rouge','user_info='.$user_info);
    }
    //活动主体
    public function rouge(){
        $user=input('user_info');
        $user=json_decode($user);
        return view('',[
            'title'=>"活动",
            'user_info'=>$user,
        ]);
    }

}

 

好的,关于微信php开发包和TP5专用微信sdk的使用教程,我可以给您提供一些基本的信息和建议。 首先,微信php开发包是一个开源的PHP类库,主要用于实现微信公众号开发中的各种功能,包括公众号的认证、消息的接收和回复、素材管理、用户管理等等。该类库基于微信官方API进行封装,使用起来比较方便,适合PHP开发者快速开发微信公众号应用。 而TP5专用微信sdk则是一个基于微信php开发包封装的TP5扩展包,可以直接在TP5框架中使用,提供了更加简便的接口和操作方法。使用该扩展包可以大大简化微信公众号开发的流程,快速实现各种功能。 下面是使用教程的一些基本步骤: 1. 首先,您需要在微信公众平台上申请并获得公众号的appid和appsecret,以及token等基本信息。 2. 然后,您可以下载微信php开发包和TP5专用微信sdk,将其放置在您的项目目录下。 3. 在TP5框架中,您需要在config目录下创建一个wechat.php的配置文件,用于存储微信公众号的基本信息和配置。 4. 在控制器中,您可以使用如下代码初始化微信sdk: ``` use think\facade\Config; use think\facade\Request; use wechat\Wechat; class WechatController extends Controller { protected $wechat; public function __construct() { parent::__construct(); $config = Config::get('wechat'); $this->wechat = new Wechat($config); } } ``` 其中,$config变量是您在config/wechat.php中定义的微信公众号配置信息。 5. 接下来,您可以根据需要使用微信sdk提供的各种功能。例如,接收用户发送的消息并回复: ``` public function index() { $request = Request::instance(); if ($request->isGet()) { // 验证消息的确来自微信服务器 $this->wechat->valid(); } else { // 处理用户发送的消息 $message = $this->wechat->getMessage(); if ($message) { $this->wechat->reply($message->Content); } } } ``` 在以上代码中,我们使用了valid()方法用于验证消息是否来自微信服务器,getMessage()方法用于获取用户发送的消息,reply()方法用于回复消息。 当然,以上只是一个简单的示例,您可以根据自己的需求选择合适的方法和接口进行开发。同时,建议您仔细阅读微信php开发包和TP5专用微信sdk的官方文档,了解更多详细信息和使用方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值