Home  >  Article  >  WeChat Applet  >  Introduction to the method of generating parameter QR code by mini program

Introduction to the method of generating parameter QR code by mini program

青灯夜游
青灯夜游forward
2020-04-30 17:15:113882browse

How does the WeChat applet generate parameter QR code? The following article will introduce to you how a small program generates parameter QR codes. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Introduction to the method of generating parameter QR code by mini program

Parameter QR code, as the name suggests, is a QR code with parameters. Because the parameters are not fixed, the QR code with parameters needs to change according to the parameters, and different QR codes are generated according to different parameters.

Parameter QR code is used in many small programs. For example, if it is a small program for making resumes, it is likely that users need to provide the following functions: upload their own resumes and generate their own QR codes. For another example, a small program for online photo album production needs to provide a QR code to the user after the user completes the production, so that anyone can scan the code to view the content of the album. These are the real application scenarios of parametric QR codes. In today's mini program development tutorial, we will explain how the WeChat mini program generates parameter QR codes.

Introduction to the method of generating parameter QR code by mini program

First of all, WeChat’s official interface address for obtaining the mini program page api is as follows:

https://api.weixin.qq.com/ cgi-bin/wxapp/createwxaqrcode?access_token=ACCESS_TOKEN

Since the help provided by the mini program parameter QR code API is limited, the following is my operation method. I mainly implement it through the backend interface of thinkphp. The specific code is as follows:

Step 1, first obtain ACCESS_TOKEN

$tokenUrl=https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=.$this->appid.&secret=.$this->secret;
        $getArr=array();
        $tokenArr=json_decode($this->send_post($tokenUrl,$getArr,GET));
 
        $access_token=$tokenArr->access_token;

send_post:

function send_post($url, $post_data,$method=\'POST\') {
        $postdata = http_build_query($post_data);
        $options = array(
            \'http\' => array(
                \'method\' => $method, //or GET
                \'header\' => \'Content-type:application/x-www-form-urlencoded\',
                \'content\' => $postdata,
                \'timeout\' => 15 * 60 // 超时时间(单位:s)
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
 
 
        return $result;
    }

Step 2, generate the QR code:

$path=pages/index?query=1;
        $width=430;
        $post_data=\'{path:\'.$path.\',width:\'.$width.\'}\';
 
        $url=https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=.$access_token;
 
        $result=$this->api_notice_increment($url,$post_data);

api_notice_increment:

function api_notice_increment($url, $data){
        $ch = curl_init();
        $header = Accept-Charset: utf-8;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, \'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)\');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        //         var_dump($tmpInfo);
        //        exit;
        if (curl_errno($ch)) {
            return false;
        }else{
            // var_dump($tmpInfo);
            return $tmpInfo;
        }
    }

Step three, generate an image based on the returned binary data and upload it to your own server

file_put_contents($filepath, $result)

Different people use different servers. This involves privacy, so I won’t post the upload code.

Introduction to the method of generating parameter QR code by mini program

Recommended: "小program Development Tutorial"

The above is the detailed content of Introduction to the method of generating parameter QR code by mini program. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jisuapp.cn. If there is any infringement, please contact admin@php.cn delete