微信公众平台开发 批量生成及下载带参二维码图片

来源:互联网 发布:淘宝pc网址转换无线端 编辑:程序博客网 时间:2024/05/01 13:52

微信公众平台开发 批量生成及下载带参二维码图片

最近在做微信二次开发时经常会使用到场景二维码,所以就把相关的批量生成并保存二维码图片的代码整理了一下。
文章的主要内容:

  1. 提供批量生成及下载二维码图片的php代码(对accessToken做了本地保存处理,不会出现请求次数饱和的现象)
  2. 制作二维码过程中需要注意的小细节

在微信公众平台上如何使用高级接口开发生成带参数二维码的功能,网上有很多相关教程。可以参考微信官方文档_生成带参数的二维码 和方倍老师的博客 微信公众平台开发(83) 生成带参数二维码 来了解制作的原理和流程。
获取ticket流程图

Talk is cheap , show you the code now:

  1. 执行方法:
    /**     * 参数说明:     * type:二维码类型  1:永久 2:临时     * from:二维码所带参数开始值     * to :二维码所带参数结束值     */    public function creat_qrcode_image() {        $scene_type = $_REQUEST ['type'];        switch($scene_type)        {            case '1': //永久                $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';                break;            case '2': //临时                $data = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';                break;            default:                $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';                break;        }        $access_token = $this->getAccessToken();        for($scene_id = 101; $scene_id <= 120; $scene_id ++) {            $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $access_token;            $res = $this->https_request ( $url, $data );            $result = json_decode ( $res, true );            $ret [$scene_id] = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode ( $result ["ticket"] );            $imageInfo = $this->downloadQrImage ( $ret [$scene_id] );            // 写入,生成图片            $filename = ROOT . "/Files/Wechat/qrcode_".$scene_id.".jpg";            $local_file = fopen ( $filename, 'w' );            if (false !== $local_file) {                if (false !== fwrite ( $local_file, $imageInfo ["body"] )) {                    fclose ( $local_file );                }            }        }    }

2 . 获取accessToken (需填入你的appId 和 appSecret)

public function getAccessToken() {        $appId='等你来填';        $appSecret='等你来填';        $filename='access_token.json';        $data = json_decode(file_get_contents(ROOT ."/Files/Wechat/".$filename));        if ($data->expire_time < time()) {            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";            $res = json_decode($this->https_request($url));            $access_token = $res->access_token;            if ($access_token) {                $data->expire_time = time() + 7000;                $data->access_token = $access_token;                $fp = fopen(ROOT ."/Files/Wechat/".$filename, "w");                fwrite($fp, json_encode($data));                fclose($fp);            }        } else {            $access_token = $data->access_token;        }        return $access_token;    }

使用说明:

1.在getAccessToken()方法中填入你的appId 和 appSecret。

2.执行creat_qrcode_image()方法。

eg. 地址栏输入 http://www.abcd.com/creat_qrcode_image?type=1&from=100&to=120
二维码图片会自动保存在你设置的目录下。

    值得注意的是,scene_id不能改成其他键名,如m_id等,否则生成的二维码被扫描后,得到$object->EventKey就是qrscene_0    scene_id只能为**数字**,出现字母则结果为0    "scene_id":0001  ,得到的结果为qrscene_1

这里提供一个实用的封装过的echo()方法:

function e(){        echo "<pre>";        $objs = func_get_args();        foreach($objs as $obj) {print_r($obj);echo "<br>";}        echo "</pre>";    }function _e(){    echo "<pre>";    $objs = func_get_args();    foreach($objs as $obj) {print_r($obj);echo "<br>";}    echo "</pre>";    exit;}

使用方法:e ($ret); _e($ret);
这两个对象输出方法可以自动按格式输出string、array、object等类型的对象。
e()方法只做打印输出操作。_e()方法的在打印输出操作后停止执行该方法。


0 0
原创粉丝点击