Obtain the QR code of the WeChat applet

You can scan the QR code of the mini program directly through WeChat to enter the mini program directly, which can be used for sharing and promoting business.

Table of contents

Curl request method

Get mini program token

Get the QR code of the mini program

Parameter Description

Notice

Request results


Curl request method

It is necessary to request the API interface of the WeChat applet and encapsulate the curl request method.

code show as below:

/**
 * 请求接口返回内容
 * @param $url :请求的URL地址
 * @param $method :请求方式POST|GET
 * @param $params :请求的参数
 * @param $header : 请求头
 * @return bool|string
 */
protected function linkCurl($url, $method, $params = array(), $header = array())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_FAILONERROR, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if (strpos("$" . $url, "https://") == 1) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    if ($method == "POST") {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    } else if ($params) {
        curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
    }
    $response = curl_exec($ch);
    if ($response === false) {
        return false;
    }
    curl_close($ch);
    return $response;
}

Get mini program token

Obtain the access_token of the mini program through the mini program's Appid and secret.

After getting the access_token, you can call the functional interface of the mini program.

code show as below:

/**
 * 获取电子签小程序接口调用凭证
 * @return mixed
 */
protected function contractMiniAccessToken()
{
    $APPID = ‘小程序appid’;
    $SECRET = ‘小程序secret’;
    $url = "https://api.weixin.qq.com/cgi-bin/token?appid={$APPID}&secret={$SECRET}&grant_type=client_credential";
    $res = $this->linkCurl($url, 'GET');
    $res = djson($res);
    if (isset($res['errcode']) && $res['errcode']) {
        return toFail($res['errmsg']);
    }

    return toSuccess('success', $res['access_token']);
}

Get the QR code of the mini program

Parameter Description

Two parameters:

path: required, the path to open the mini program after scanning;

width: optional, the width of the image, the default is 430.

After success, the errcode is not returned. What is returned is the binary content of the QR code image. It needs to be written to a file, saved locally on the server and finally returned to the front end.

Notice

If the binary content of the image is returned directly, it will only respond blank.

code show as below:

**
 * 获取小程序二维码
 * @return array|mixed
 */
public function createQRCode()
{
    $fileName = './uploads/contractQr.png';
    if(!file_exists($fileName)) {
        $res = $this->contractMiniAccessToken();
        if ($res['status'] != 1) return $res;

        $url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=' . $res['data'];
        $param = [
            'path' => 'pages/index/index',
            'width' => 230,
        ];
        $data = json_encode($param);
        $header = array();
        $header[] = 'content-type:application/json';
        $info = $this->linkCurl($url, 'POST', $data, $header);
        if (strlen($info) < 300) {
            $res = djson($res);
            if (isset($res['errcode']) && $res['errcode']) {
                return toFail($res['errmsg']);
            }
        }
        file_put_contents($fileName, $info);
    }

    return toSuccess('success', ['img' => getSolveUrl($fileName)]);
}

Request results

{
    "status":1,
    "message":"success",
    "data":{
        "img":"http://new.solveset.com/uploads/contractQr.png"
    },
    "total":0
}

QR code example

Guess you like

Origin blog.csdn.net/json_ligege/article/details/134461755