微信批量生成二维码并保存

67 篇文章 0 订阅
49 篇文章 0 订阅
本人小菜鸟一只,为了自我学习和交流PHP(jquery,linux,lamp,shell,javascript,服务器)等一系列的知识,小菜鸟创建了一个群。希望光临本博客的人可以进来交流。寻求共同发展。搭建平台。本人博客也有许多的技术文档,希望可以为你提供一些帮助。

QQ群: 191848169 QQ:450225664


直接上Demo:



<?php
header("Content-type: text/html; charset=utf-8");
//构建二维码参数
$data = array(
        '4001',
        '4002',
        '4003',
        '4004',
        '4005',
        '4006',
        '4007',
        '4008',
        '4009',
        '4010',
        '4011',
        '4012',
        '4013',
        '4014',
        '4015',
        );
//循环创建二维码
$AppId = '';//appid  填写自己的appid
$AppSecret = ' '; //AppSecret  填写自己的AppSecret
$Access_Token = get_access_token($AppId,$AppSecret); //获取token

if(empty($Access_Token)){
    exit('access_token获取失败!');
}
foreach($data as $val){
    $filename = $val.'_qrcode.jpg'; //新文件名字
    if(!file_exists('images/')){
        mkdir ("./images");//创建二维码存放文件夹
    }else{
        if(!file_exists('images/'.$filename)){
        //永久二维码
        $qrcode = '{"action_name": "QR_LIMIT_SCENE","action_info": {"scene": {"scene_id":'.$val.'}}}';
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$Access_Token";
        $resulstd = https_post($url,$qrcode);
        $jsoninfo = json_decode($resulstd,true);
        $ticket = $jsoninfo['ticket'];
        //下载二维码图片
        $urls = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
        $imageinfo = downloadimagefromweixin($urls);
        $local_file = fopen('./images/'.$filename, 'w');
        //如果没有打开文件,进行写入操作
            if(false !==$local_file){
                if(false !==fwrite($local_file, $imageinfo['body'])){
                    fclose($local_file);
                }
            }
        }else{
            //已经存在的二维码不执行
        }
    }
}
/**
 * 获取token
 */
function get_access_token($AppId,$AppSecret){
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$AppId.'&secret='.$AppSecret;
    $result = json_decode(file_get_contents($url));
    //可以缓存起来 expires_in 过期时间 access_token
    return $result->access_token;
    
}
function https_post($url,$data=null){
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
    if(!empty($data)){
        curl_setopt($curl, CURLOPT_POST,1);
        curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
    }
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}
//获得二维码图片
function downloadimagefromweixin($url){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch, CURLOPT_NOBODY,0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $package = curl_exec($ch);
    $httpinfo = curl_getinfo($ch);
    curl_close($ch);
    return array_merge(array('body'=>$package),array('header'=>$httpinfo));
}
echo '二维码批量生成成功';
?>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值