Perl获取微信小程序二维码

8 篇文章 0 订阅

微信小程序API:

https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html

 

# 参数 $path即页面路径,比如:"pages/index?query=1"

sub get_wxa_code {

    my ($fid, $path) = @_;

 

    my $ret = get_access_token();  #获取access_token

    my $image_fid = "";

    my $json = JSON->new();

    if (length($ret->{access_token})) {

        my $post_data;

        $post_data->{path} = $path;

        #$post_data->{width} = 430;

        #$post_data->{auth_color} = 1;

        #$post_data->{line_color} = {"r"=>"0","g"=>"0","b"=>"0"};

 

        my $param_json_str =$json->encode($post_data);

        my $header = HTTP::Headers->new( Content_Type => 'application/json; charset=utf8', );

        my $http_request = HTTP::Request->new( POST => "https://api.weixin.qq.com/wxa/getwxacode?access_token=".$ret->{access_token}, $header, $param_json_str );

        my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0, SSL_verify_mode => 0x00 });

        $ua->timeout(3);

        my $response = $ua->request($http_request);

        if ($response->message ne "OK" && $response->is_success ne "1") { #出错,或者timeout了

                return "timeout";

        } else { # 如果成功,则返回的是图片对应的二进制内容,这里直接写到文件保存

                my $file = "/var/www/wechat/files/".$fid;   #目录要有读写权限

                open(LOCAL, ">$file");

                binmode(LOCAL); #内容为二进制格式

                print LOCAL $response->content();

                close LOCAL;

                if (-s $file ) { #判断是否写成功

                        $image_fid = $fid;

                } else {

                        return "write file failed";

                }

        }

    } else {

        return "get access_token failed";

    }

    return $image_fid;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值