PHP 抖音小程序二维码生成

使用的是tp框架

转自 ThinkPHP5生成抖音小程序带参数二维码_菜鸟何时能翻身-CSDN博客_抖音小程序二维码生成

1. 获取accesstoken

1 public function get_access_token()
2 {
3     $appid = 'tt301******3a416'; //配置appid
4     $secret = '0421128******65529'; //配置secret
5     $url = "https://developer.toutiao.com/api/apps/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
6     $result = https_request($url);
7     $Result = json_decode($result, true);
8     return $Result['access_token'];
9 }

2. 生成带参数二维码

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

 1 //获得抖音二维码
 2 public function dyshare()
 3 {
 4 
 5     $spcode = request()->param('spcode', '');
 6     $code = Db::name('newuser')->where('spcode', $spcode)->find();
 7     if (!$code) {
 8         return json(['code' => 400, 'message' => '用户不存在', 'data' => '']);
 9     }
10     if ($code['dyfxcode'] != '') {
11         return json(['code' => 200, 'message' => '获取成功', 'data' => request()->domain() . $code['dyfxcode']]);
12     }
13 
14     $qr_path = "./uploads/";
15     if (!file_exists($qr_path . 'user/')) {
16         mkdir($qr_path . 'user/', 0700, true); //判断保存目录是否存在,不存在自动生成文件目录
17     }
18     $filename = 'user/' . time() . '.png';
19     $file = $qr_path . $filename;
20     $access_token = $this->get_access_token();
21     $url = 'https://developer.toutiao.com/api/apps/qrcode';
22 
23     $qrcode = array(
24         'access_token' => $access_token,
25         'scene'         => $spcode, //二维码所带参数
26         'appname'       => 'douyin',
27         // 'width'         => '430',
28         'path'          => urlencode("pages/index/index?spcode=" . $spcode), //二维码跳转路径(要已发布小程序)
29         // 'platform'      => 'miniapp',
30         // "set_icon" => true
31     );
32     $data = https_post($url, json_encode($qrcode)); //请求接口
33     $filename = date('YmdHis') . rand(10000, 999999) . '.jpg';
34     $dir = ROOT_PATH . 'public/uploads/douyin';
35     if (!is_dir($dir)) {
36         @mkdir($dir, 0777, true);
37     }
38 
39     $file = $dir . '/' . $filename;
40     file_put_contents($file, $data);
41 
42     $retFile = '/uploads/douyin/' . $filename;
43 
44     Db::name('newuser')->where('spcode', $spcode)->update(['dyfxcode' => $retFile]);
45 
46     return json(['code' => 200, 'message' => '获取成功', 'data' => request()->domain() . $retFile]);
47 }

 

2.2 直接返回图片

 1 //获得抖音二维码
 2 public function dyshare()
 3 {
 4     $spcode = request()->param('spcode', '');
 5     $access_token = $this->get_access_token();
 6     $url = 'https://developer.toutiao.com/api/apps/qrcode';
 7     $qrcode = array(
 8         'access_token' => $access_token,
 9         'scene'         => $spcode, //二维码所带参数
10         'appname'       => 'douyin',
11         // 'width'         => '430',
12         'path'          => urlencode("pages/index/index?spcode=" . $spcode), //二维码跳转路径(要已发布小程序)
13         // 'platform'      => 'miniapp',
14         // "set_icon" => true
15     );
16     $data = https_post($url, json_encode($qrcode)); //请求接口
17     return response($data, 200, ['Content-Length' => strlen($data)])->contentType('image/png');
18 }

 

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

 1 #curl请求
 2 if (!function_exists('https_request')) {
 3     function https_request($url, $data = null)
 4     {
 5         if (function_exists('curl_init')) {
 6             $curl = curl_init();
 7             curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
 8             curl_setopt($curl, CURLOPT_URL, $url);
 9             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
10             curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
11             curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
12             curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
13             if (!empty($data)) {
14                 curl_setopt($curl, CURLOPT_POST, 1);
15                 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
16             }
17             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
18             $output = curl_exec($curl);
19             curl_close($curl);
20             return $output;
21         } else {
22             return false;
23         }
24     }
25 }
26 #post请求
27 if (!function_exists('https_post')) {
28     function https_post($url, $data)
29     {
30         $curl = curl_init();
31         curl_setopt($curl, CURLOPT_URL, $url);
32         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
33         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
34         curl_setopt($curl, CURLOPT_POST, 1);
35         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
36         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
37         curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));
38         // dump($curl);
39         $result = curl_exec($curl);
40         if (curl_errno($curl)) {
41             return 'Errno' . curl_errno($curl);
42         }
43         curl_close($curl);
44         return $result;
45     }
46 }

 

posted @ 2021-06-17 13:34  九鹤  阅读(1147)  评论(0编辑  收藏  举报