php教程

ThinkPHP5生成抖音小程序二维码示例代码

我的站长站 2022-07-26 人阅读

获取accesstoken示例代码

官方文档:https://microapp.bytedance.com/dev/cn/mini-app/develop/server/interface-request-credential/getaccesstoken

public function get_access_token(){
        $appid = 'tt4a8******2271f';//配置appid
        $secret = '420d0ae397099**********9a97017d09378';//配置secret
        $url = "https://developer.toutiao.com/api/apps/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
        $result = https_request($url);
        $Result = json_decode($result,true);
        return $Result['access_token'];
    }

生成带参数二维码示例代码

存到服务器本地+路径存到用户指定字段用于二次分享的时候直接拿取对应带参数二维码

官方文档: https://microapp.bytedance.com/dev/cn/mini-app/develop/server/qr-code/createqrcode

 //获得抖音二维码
    public function dyshare(){
        if (!input('?post.spcode')) {
            $ret['code'] = 400;
            $ret['message'] = "缺少参数,请参照接口文档,规范传参";
            $ret['data'] = null;
            return json($ret);
        }
        $spcode=request()->param('spcode');
        $code=Db::name('newuser')->where('spcode',$spcode)->find();
        if(!$code){
            return json(['code'=>400,'message'=>'用户不存在','data'=>'']);
        }
        if($code['dyfxcode']!=''){
            return json(['code'=>200,'message'=>'获取成功','data'=>request()->domain().$code['dyfxcode']]);
        }
        $qr_path = "./uploads/";
        if(!file_exists($qr_path.'user/')){
            mkdir($qr_path.'user/', 0700,true);//判断保存目录是否存在,不存在自动生成文件目录
        }
        $filename = 'user/'.time().'.png';
        $file = $qr_path.$filename;
        $access_token= $this->get_access_token();
        $url = 'https://developer.toutiao.com/api/apps/qrcode';
        $qrcode = array(
            'access_token'=> $access_token,
            'scene'         => $spcode,//二维码所带参数
            'appname'       => 'douyin',
            // 'width'         => '430',
             'path'          => urlencode("pages/index/index?spcode=".$spcode),//二维码跳转路径(要已发布小程序)
            // 'platform'      => 'miniapp',
            // "set_icon" => true
        );
        $data = $this->https_post($url,json_encode($qrcode));//请求接口
        $filename = date('YmdHis').rand(10000,999999).'.jpg';
        $dir = ROOT_PATH.'public/uploads/douyin';
        if(!is_dir($dir)){
            @mkdir($dir,0777,true);
        }
        $file = $dir.'/'.$filename;
        file_put_contents($file,$data);
        $retFile ='/uploads/douyin/'.$filename;
        Db::name('newuser')->where('spcode',$spcode)->update(['dyfxcode'=>$retFile]);
        return json(['code'=>200,'message'=>'获取成功','data'=>request()->domain().$retFile]);
    }

httpGet请求,可以直接放在公共函数库application/common.php里面

#curl请求示例代码

if(!function_exists('https_request')){
    function https_request($url,$data = null){
        if(function_exists('curl_init')){
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1);
            if (!empty($data)){
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($curl);
            curl_close($curl);
            return $output;
        }else{
            return false;
        }
    }
}
最新更新