溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序如何生成帶參數(shù)的小程序二維碼

發(fā)布時(shí)間:2021-10-19 17:59:39 來(lái)源:億速云 閱讀:238 作者:柒染 欄目:大數(shù)據(jù)

微信小程序如何生成帶參數(shù)的小程序二維碼,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

官方提供生成小程序碼的兩種方式:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html

服務(wù)端生成小程序碼

//小程序碼
public function getWxcode()
{
    $ACCESS_TOKEN = $this->getWxAccessToken();
    $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $ACCESS_TOKEN['access_token'];
    $post_data =
        array(
            'page' => 'pages/my/my', //掃碼跳轉(zhuǎn)頁(yè)面
            'scene' => input('invite_code') //用戶邀請(qǐng)碼
        );
    $post_data = json_encode($post_data);
    $data = $this->send_post($url, $post_data);
    $result = $this->data_uri($data, 'image/png');
    return '<image src='.$result.'></image>';
    //return $result;
}

private function getWxAccessToken()
{
    $appid = '******';
    $appsecret = '*******';
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
    $access_token = $this->makeRequest($url);
    $access_token = json_decode($access_token['result'], true);
    return $access_token;

}

/**
 * 發(fā)起http請(qǐng)求
 * @param string $url 訪問(wèn)路徑
 * @param array $params 參數(shù),該數(shù)組多于1個(gè),表示為POST
 * @param int $expire 請(qǐng)求超時(shí)時(shí)間
 * @param array $extend 請(qǐng)求偽造包頭參數(shù)
 * @param string $hostIp HOST的地址
 * @return array    返回的為一個(gè)請(qǐng)求狀態(tài),一個(gè)內(nèi)容
 */
private function makeRequest($url, $params = array(), $expire = 0, $extend = array(), $hostIp = '')
{
    if (empty($url)) {
        return array('code' => '100');
    }

    $_curl = curl_init();
    $_header = array(
        'Accept-Language: zh-CN',
        'Connection: Keep-Alive',
        'Cache-Control: no-cache'
    );
    // 方便直接訪問(wèn)要設(shè)置host的地址
    if (!empty($hostIp)) {
        $urlInfo = parse_url($url);
        if (empty($urlInfo['host'])) {
            $urlInfo['host'] = substr(DOMAIN, 7, -1);
            $url = "http://{$hostIp}{$url}";
        } else {
            $url = str_replace($urlInfo['host'], $hostIp, $url);
        }
        $_header[] = "Host: {$urlInfo['host']}";
    }

    // 只要第二個(gè)參數(shù)傳了值之后,就是POST的
    if (!empty($params)) {
        curl_setopt($_curl, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($_curl, CURLOPT_POST, true);
    }

    if (substr($url, 0, 8) == 'https://') {
        curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    curl_setopt($_curl, CURLOPT_URL, $url);
    curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($_curl, CURLOPT_USERAGENT, 'API PHP CURL');
    curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header);

    if ($expire > 0) {
        curl_setopt($_curl, CURLOPT_TIMEOUT, $expire); // 處理超時(shí)時(shí)間
        curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 建立連接超時(shí)時(shí)間
    }

    // 額外的配置
    if (!empty($extend)) {
        curl_setopt_array($_curl, $extend);
    }

    $result['result'] = curl_exec($_curl);
    $result['code'] = curl_getinfo($_curl, CURLINFO_HTTP_CODE);
    $result['info'] = curl_getinfo($_curl);
    if ($result['result'] === false) {
        $result['result'] = curl_error($_curl);
        $result['code'] = -curl_errno($_curl);
    }

    curl_close($_curl);
    return $result;
}

/**
 * 消息推送http
 * @param $url
 * @param $post_data
 * @return bool|string
 */
private function send_post($url, $post_data)
{
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/json',
            //header 需要設(shè)置為 JSON
            'content' => $post_data,
            'timeout' => 60
            //超時(shí)時(shí)間
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}

//二進(jìn)制轉(zhuǎn)圖片image/png
private function data_uri($contents, $mime)
{
    $base64 = base64_encode($contents);
    return ('data:' . $mime . ';base64,' . $base64);
}

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI