19

thinkphp5获取微信小程序码,以wxacode.get为例

 3 years ago
source link: https://xushanxiang.com/2020/08/thinkphp5-wxacode-get.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

thinkphp5获取微信小程序码,以wxacode.get为例

作者: xusx 分类: 小程序 发布时间: 2020-08-20 23:17

通过后台接口可以获取小程序任意页面的小程序码,扫描该小程序码可以直接进入小程序对应的页面,所有生成的小程序码永久有效,可放心使用。 推荐生成并使用小程序码,它具有更好的辨识度,且拥有展示“公众号关注组件”等高级能力。 

wxacode.png

微信提供三个服务端(https://developers.weixin.qq.com/miniprogram/dev/api-backend/)接口来获取小程序(二维)码,分别是:

1、wxacode.createQRCode(不推荐使用)

获取小程序二维码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制。

生成二维码,可接受 path 参数较长,生成个数受限。

2、wxacode.get*

获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制。

适用于需要的码数量较少的业务场景。

生成小程序码,可接受 path 参数较长,生成个数受限,接口 1 加上接口 2,总共生成的码数量限制为 100,000(十万)个,请谨慎调用。

3、wxacode.getUnlimited

获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。

适用于需要的码数量极多的业务场景。

生成小程序码,可接受页面参数较短,生成个数不受限。

接口 3 调用分钟频率受限(5000次/分钟),如需大量小程序码,建议预生成。

注意:接口只能生成已发布的小程序(二维)码。另外,“微信一物一码”也支持生成小程序码。

二、wxacode.get接口

本文以wxacode.get接口调用来做详细过程说明,其他两个接口流程也基本一样。不多说,这就上菜,代码如下:

 // 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.get.html

function wxacode()
    {
        $access_token = cache('access_token_consumer');
        if(!$access_token){ // 检查 接口调用凭证 缓存
            $access_token_info = get_access_token();
            if($access_token_info['errcode']!=0){
                return json([
                    'errcode' => $access_token_info['errcode']
                ]);
            }
            $access_token = $access_token_info['access_token'];
        }
        $queryString  = [
            'path'  => '/pages/home/index?s=421224000001',
            'width' => 300
        ];
        $url          = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
        $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);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($queryString));
        // POST 参数需要转成 JSON 字符串,不支持 form 表单提交
        $output = curl_exec($ch);
        if(curl_errno($ch)){
            curl_close($ch);
            return json(['errcode'=>2]);
        }
        curl_close($ch);
        if(!is_null(json_decode($output))){
            return json(['errcode'=>3]);
        }
        $_path = ROOT_PATH . 'public' . DS . 'uploads/img/index.jpg';
        $file = fopen($_path, 'w');
        fwrite($file, $output); // 保存图片
        fclose($file);

        $im = imagecreatefromjpeg($_path);

        // ob_start();
        // imagejpeg($im);
        // $content = ob_get_clean();
        // imagedestroy($im);
        // response($content, 200, ['Content-Length'=>strlen($content)])->contentType('image/jpeg');

        header('Content-Type: image/jpeg');
        imagejpeg($im); // 浏览器直接输出图片
        exit(); // 一定不能少
    }
// 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html

function get_access_token()
    {
        $queryString  = [
            'grant_type' => 'client_credential',
            'appid'      => 'wx......',
            'secret'     => '......'
        ];
        $url          = 'https://api.weixin.qq.com/cgi-bin/token?'.http_build_query($queryString);
        $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);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        $output = curl_exec($ch);
        if(curl_errno($ch)){
            curl_close($ch);
            return ['errcode'=>2];
        }
        curl_close($ch);
        // echo $output;
        // die('');
        $output = json_decode($output, true);
        if(isset($output['access_token'])){
            cache('access_token_consumer', $output['access_token'], 3600); // 缓存最多7200秒
            return [
                'errcode'      => 0,
                'access_token' => $output['access_token']
            ];
        }else{
            return ['errcode'=>2];
        }
    }

是不是很方便呢?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK