zoukankan      html  css  js  c++  java
  • php生成微信小程序二维码源码

    目前有3个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口。
    第一步:获取   access_token 

    public function getWxAccessToken(){
        $appid='wxbcfaa005da7b16**';
        $appsecret='****';
        if(Session::get('access_token_'.$appid) && Session::get('expire_time_'.$appid)>time()){
            return Session::get('access_token_'.$appid);
        }else{
            $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);
            Session::set('access_token_'.$appid,$access_token);
            Session::set('expire_time_'.$appid,time()+7000);
            return $access_token;
        }
    }
    /**
     * 发起http请求
     * @param string $url 访问路径
     * @param array $params 参数,该数组多于1个,表示为POST
     * @param int $expire 请求超时时间
     * @param array $extend 请求伪造包头参数
     * @param string $hostIp HOST的地址
     * @return array    返回的为一个请求状态,一个内容
     */
    public 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'
        );
        // 方便直接访问要设置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']}";
        }
    
        // 只要第二个参数传了值之后,就是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); // 处理超时时间
            curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 建立连接超时时间
        }
    
        // 额外的配置
        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;
    }

    第二步:调用接口生成微信二维码(这里以接口B为例)

    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/caregory/index',
                'scene'=>'34,S853EE4QRP'//34%2CS853EE4QRP
            );
            $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>';
        }
     /**
         * 消息推送http
         * @param $url
         * @param $post_data
         * @return bool|string
         */
        protected function send_post( $url, $post_data ) {
            $options = array(
                'http' => array(
                    'method'  => 'POST',
                    'header'  => 'Content-type:application/json',
                    //header 需要设置为 JSON
                    'content' => $post_data,
                    'timeout' => 60
                    //超时时间
                )
            );
            $context = stream_context_create( $options );
            $result = file_get_contents( $url, false, $context );
            return $result;
        }
    //二进制转图片image/png
    public function data_uri($contents, $mime)
    {
        $base64   = base64_encode($contents);
        return ('data:' . $mime . ';base64,' . $base64);
    }


    接口A: 适用于需要的码数量较少的业务场景 接口地址:

    https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN  

    参数如下:

    注意:通过该接口生成的小程序码,永久有效,数量限制见文末说明,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。

    接口B:适用于需要的码数量极多,或仅临时使用的业务场景:

    https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN  

    参数如下:

    注意:通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中 scene 字段的值,再做处理逻辑。使用如下代码可以获取到二维码中的 scene 字段的值。调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode

    // 这是首页的 js  
    Page({  
      onLoad: function(options) {  
        // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene  
        var scene = decodeURIComponent(options.scene)  
      }  
    })  

    接口C:适用于需要的码数量较少的业务场景: 

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

    参数如下:

    注意:通过该接口生成的小程序二维码,永久有效,数量限制见文末说明,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。

  • 相关阅读:
    android手机rootROM下载地址
    mysql alter 语句用法,添加、修改、删除字段等
    java比较两个日期大小
    eclipse设置全局编码为UTF-8的方法
    Spring MVC 异步处理请求,提高程序性能
    ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台
    maven之发布项目到nexus【clean deploy命令】
    nexus-3本地下载jar的settipng.xml配置
    windows开启3306端口并用可视化工具访问远程mysql(授权访问)
    mysql 列转行,合并字段
  • 原文地址:https://www.cnblogs.com/mverting/p/9066198.html
Copyright © 2011-2022 走看看