Perl gets WeChat applet QR code

WeChat Mini Program API:

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

 

# The parameter $path is the page path, for example: "pages/index?query=1"

sub get_wxa_code {

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

 

    my $ret = get_access_token(); #Get  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 { # If successful, the binary content corresponding to the picture is returned, which is directly written to the file for saving

                my $file = "/var/www/wechat/files/".$fid; #The   directory must have read and write permissions 

                open(LOCAL, ">$file");

                binmode(LOCAL); #The content is in binary format

                print LOCAL $response->content();

                close LOCAL;

                if (-s $file ) { #Determine whether the write is successful

                        $ image_fid = $ fid;

                } else {

                        return "write file failed";

                }

        }

    } else {

        return "get access_token failed";

    }

    return $image_fid;

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326127456&siteId=291194637