微信-JSSDK网页调用-(微信扫一扫)
阅读原文时间:2023年07月08日阅读:3

网页调用微信扫一扫接口

1、准备工作:

   1.1微信浏览器

1.2微信APPID,nonceStr

2、使用方式快速预览

调用扫一扫微信接口。

1需要获取access_token

2获取 $jsapi_ticket

3加密

4返回前端

5前端Js实现接口

3、错误,坑点提示

1、获取的access_token 一定要加缓存,原因是有次数限制,3000

2、组装的url 地址一定要全。 http,https一定 一定不能少

3、Js安全接口一定要配置到域名地址。

二、代码实现

JSSDK类,提供大家参考,建议大家写成一个完整的类包,方便管理,和运用

<?php
/**
* Created by PhpStorm.
* User: JOJO
* Date: 2019/8/4
* Time: 14:53
*/

class JSSDK
{
public $appId;
private $appSecret;

/\*\*  
 \* @name         初始化参数  
 \* @author       JOJO  
 \* @copyright  
 \*/  
public function \_\_construct($appId, $appSecret)  
{

    $this->appId = $appId;

    $this->appSecret = $appSecret;  
}

/\*\*  
 \* @name         获取config接口注入权限验证配置  
 \* @author       JOJO  
 \* @copyright  
 \*/  
public function getWxConfig()  
{

    $jsapiTicket = $this->getJsapiTicket();  
    # - 获取调用页面的url  
    $protocol = (!empty($\_SERVER\['HTTPS'\]) && $\_SERVER\['HTTPS'\] !== 'off' || $\_SERVER\['SERVER\_PORT'\] == 443) ? "https://" : "http://";  
    $url = "$protocol$\_SERVER\[HTTP\_HOST\]$\_SERVER\[REQUEST\_URI\]";  
    # - 时间戳  
    $timestamp = time();

    # - 获取随机字符串  
    $nonceStr = $this->createNonceStr();

    # - 这里参数的顺序要按照 key 值 ASCII 码升序排序  
    # - 亦可把参数以数组存值,ksort() - 以升序对关联数组进行排序  
    $string = "jsapi\_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

    $signature = sha1($string);

    # - 页面所需注入参数  
    $WxConfig = array(  
        "appId" => $this->appId,  
        "nonceStr" => $nonceStr,  
        "timestamp" => $timestamp,  
        "url" => $url,  
        "signature" => $signature,  
        "rawString" => $string  
    );

    return $WxConfig;  
}

public function getJsapiTicket()  
{  
    if (\\Yii::$app->cache->exists('jsapi\_ticket')) {  
        return \\Yii::$app->cache->get('jsapi\_ticket');  
    }  
    $access\_token = $this->getAccessToken();  
    if ($access\_token == false) {  
        return false;  
    }  
    $url = sprintf("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access\_token=%s&type=jsapi", $access\_token);  
    $res = wx\_tools::getCurl($url);  
    $res = json\_decode($res, true);  
    if (isset($res\['errcode'\]) && $res\['errcode'\] != 0) {  
        return false;  
    }  
    //这里应该把access\_token缓存起来,有效期是7200s  
    \\Yii::$app->cache->set('jsapi\_ticket', $res\['ticket'\], 7000);  
    return $res\['ticket'\];  
}

public function getAccessToken()  
{  
    if (\\Yii::$app->cache->exists('access\_token')) {  
        return \\Yii::$app->cache->get('access\_token');  
    }  
    $AppID =$this->appId;//AppID(应用ID)  
    $AppSecret = this->appSecret;//AppSecret(应用密钥)  
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant\_type=client\_credential&appid=' . $AppID . '&secret=' . $AppSecret;  
    $res = $this->getCurl($url);  
    $res = json\_decode($res, true);  
    if (isset($res\['errcode'\]) && $res\['errcode'\] != 0) {  
        return false;  
    }  
    \\Yii::$app->cache->set('access\_token', $res\['access\_token'\], 7000);  
    return $res\['access\_token'\];  
}

/\*\*  
 \* CURL GET 请求  
 \* @param $url  
 \* @return bool|mixed  
 \*/  
public  function getCurl($url)  
{  
    $curl = curl\_init();  
    if (stripos($url, "https://") !== FALSE) {  
        curl\_setopt($curl, CURLOPT\_SSL\_VERIFYPEER, FALSE);  
        curl\_setopt($curl, CURLOPT\_SSL\_VERIFYHOST, FALSE);  
        curl\_setopt($curl, CURLOPT\_SSLVERSION, 1); //CURL\_SSLVERSION\_TLSv1  
    }  
    curl\_setopt($curl, CURLOPT\_URL, $url);  
    curl\_setopt($curl, CURLOPT\_RETURNTRANSFER, 1);  
    $content = curl\_exec($curl);  
    $status = curl\_getinfo($curl);

    if (intval($status\["http\_code"\]) == 200) {  
        curl\_close($curl);  
        return $content;  
    } else {  
        $error = curl\_errno($curl);  
        curl\_close($curl);  
        file\_put\_contents('../web/logs/notify/error' . date('YmdHi') . '.txt', $error);

        return false;  
    }  
}

/\*\*  
 \* @name         产生随机字符串  
 \* @author       JOJO  
 \* @copyright  
 \*/  
private function createNonceStr($length = 16)  
{

    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    $str = "";

    for ($i = 0; $i < $length; $i++) {

        $str .= substr($chars, mt\_rand(0, strlen($chars) - 1), 1);

    }

    return $str;  
}  

}

  1、controller

$app_id = 'wx4868b98f52c67445';
$app_secret = '016c26ce85e14d19dac305fa1ba05c91';
$obj = new JSSDK($app_id, $app_secret);
$signPackage = $obj->getWxConfig();

2.view






ttt

<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">

<!--通用js-->  
<script src="<?= $web ?>/js/jquery-2.1.4.js"></script>  
<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js" charset="utf-8"></script>




手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章