获取微信小程序二维码

可直接通过微信扫描小程序二维码直接进入小程序,可用在分享推广业务。

目录

Curl请求方法

获取小程序token

获取小程序二维码

参数说明

注意

请求结果


Curl请求方法

需要请求微信小程序的API接口,封装好curl请求方法。

代码如下:

/**
 * 请求接口返回内容
 * @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;
}

获取小程序token

通过小程序的Appid和secret获取小程序的access_token。

得到access_token后就可以调用小程序的功能接口了。

代码如下:

/**
 * 获取电子签小程序接口调用凭证
 * @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']);
}

获取小程序二维码

参数说明

参数两个:

path:必填,扫描后打开小程序的路径;

width:非必填,图片的宽度,默认是430。

成功后不返回errcode,返回的是二维码图片的二进制内容,需要把它写入到文件中,保存到服务器本地最后返回给前端。

注意

如果直接返回图片二进制内容,只会响应空白。

代码如下:

**
 * 获取小程序二维码
 * @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)]);
}

请求结果

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

二维码示例

猜你喜欢

转载自blog.csdn.net/json_ligege/article/details/134461755