/*
     * 获取公众号的openid
     */
    public function test()
    {
        $param = $this->request->param();

        $config = [
            'appid' => '',//公众号的appid
            'secret' => '',//公众号的secret
        ];


        if (!isset($param['code']) || $param['code'] == '') {
            $code_data = [];
            $code_data['appid'] = $config['appid'];
            //后面的方法根据自己的情况,自行填写
            $code_data['redirect_uri'] = 'http://' . $_SERVER['HTTP_HOST'] . '/cn/pay/test?' . http_build_query($_GET);
            $code_data['response_type'] = 'code';
            $code_data['scope'] = 'snsapi_base';
            $code_data['state'] = '';
            $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($code_data) . '#wechat_redirect';
            header('location:' . $url);
        }

        $code = $param['code'];
        $openid_data = [];
        $openid_data['appid'] = $config['appid'];
        $openid_data['secret'] = $config['secret'];
        $openid_data['code'] = $code;
        $openid_data['grant_type'] = 'authorization_code';
        $openid_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' . http_build_query($openid_data);
        $res = json_decode(file_get_contents($openid_url), true);

        if (isset($res['openid'])) {
            $getUserInfo = $this->getUserInfo($res['openid']);

            if (empty($getUserInfo['unionid'])){
                $this->error('请关注公众号!');
                return;
            }else{

                $data=[
                    'openid' => $res['openid'],
                ];
                $this->success('获取成功',$data);
            }

        } else {
            $this->error('获取失败');
        }
    }




// 获取用户信息
    function getUserInfo($openid)
    {
        $accessToken = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $accessToken . "&openid=" . $openid . "&lang=zh_CN";
        $response = file_get_contents($url);
        $result = json_decode($response, true);

        return $result;
    }

// 获取 access_token
    function getAccessToken()
    {

        // 公众号的 appid 和 appsecret
        $appid = "";
        $appsecret = "";

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
        $response = file_get_contents($url);
        $result = json_decode($response, true);
        return $result['access_token'];
    }








//获取小程序openid
public function openid(){


        $code = '';//前端传
        $appid = ''; // 替换为你的小程序 appid
        $secret = ''; // 替换为你的小程序 secret

        // 拼接请求 URL
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

        // 使用 cURL 发起请求
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 如果是本地开发,可以设置为 false
        $response = curl_exec($ch);
        curl_close($ch);

        // 解析返回的 JSON 数据
        $responseData = json_decode($response, true);


    }




//获取小程序用户access_token
 public function getAccessToken() {

        $wxconfig = Db::table('pei_wxconfig')->where('type','0')->find();

        $appid = $wxconfig['appid']; // 替换为你的小程序 appid
        $secret = $wxconfig['appsecret']; // 替换为你的小程序 secret

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $res = curl_exec($curl);
        curl_close($curl);
        return json_decode($res)->access_token;
    }


 //获取小程序用户手机号
    public function getPhoneNumber() {

        $param = $this->request->param();


        $code = $param['code']; // 获取前端传递的code
        $accessToken = $this->getAccessToken(); // 获取access_token
        $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$accessToken";
        $data = ['code' => $code];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $res = curl_exec($curl);
        curl_close($curl);
        $resData = json_decode($res, true);

        if ($resData['errcode'] == 0) {


            echo json_encode(['code' => 1, 'msg' => '请求成功', 'phoneNumber' => $resData['phone_info']['phoneNumber']]);
        } else {
            $this->error('请求失败');

        }
    }





Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐