Filip Bartman Filip Bartman - 6 months ago 12
Perl Question

How do I get data from a web API?

I'm pretty basic at Perl, and I need to make a project for my university.

I want to download data from certain link, it is JSON data, so I know I have to use

module from CPAN.

But how to download content of link to my Perl variable? Should I use
LWP
get()
?

Answer

Aren't you supposed to be learning Perl if it's a university project?

Anyway, your program will look something like this. It uses the LWP::Simple module to fetch the JSON data, and then JSON::Parse to process it into a Perl data structure

I've used Data::Dumper to display the contents of the resultant data structure

use strict;
use warnings 'all';

use LWP::Simple 'get';
use JSON::Parse 'parse_json';

{
    use Data::Dumper;
    package Data::Dumper;
    our ($Terse, $Useqq, $Indent) = (1, 1, 1);
}

use constant URL => 'http://a.wykop.pl/observatory/entries/appkey,dJ4w7fXYpL';

my $json = get URL or die "Unable to get JSON data";
my $data = parse_json($json);

print Dumper $data;

output

[
  {
    "deleted" => "",
    "id" => 18039175,
    "author_avatar_med" => "http://xd.cdn02.imgwykop.pl/c3397992/avatar_def,q48.png",
    "violation_url" => undef,
    "author_avatar_big" => "http://xd.cdn02.imgwykop.pl/c3397992/avatar_def,q150.png",
    "app" => ${\$VAR1->[0]{"violation_url"}},
    "url" => "http://www.wykop.pl/wpis/18039175/arma3-najnowsze-zrzuty-ekranu-z-nowego-dodatku-do-/",
    "source" => ${\$VAR1->[0]{"violation_url"}},
    "receiver_avatar_med" => ${\$VAR1->[0]{"violation_url"}},
    "voters" => [],
    "receiver_sex" => ${\$VAR1->[0]{"violation_url"}},
    "receiver_group" => ${\$VAR1->[0]{"violation_url"}},
    "author_sex" => "male",
    "date" => "2016-06-07 17:25:32",
    "body" => "#<a href=\"#arma3\">arma3</a> <br />\nNajnowsze zrzuty ekranu z p\x{142}atnego dodatku do army 3 Apex:<br />\n<br />\n<a href=\"https://imgur.com/a/VoDCx\" rel=\"nofollow\">https://imgur.com/a/VoDCx</a><br />\n<br />\nMapa: <a href=\"https://i.imgur.com/ihKiidk.jpg\" rel=\"nofollow\">https://i.imgur.com/ihKiidk.jpg</a><br />\n<br />\nWi\x{119}cej informacji: <a href=\"https://www.youtube.com/watch?v=KnU4HxJFacw\" rel=\"nofollow\">https://www.youtube.com/watch?v=KnU4HxJFacw</a><br />\n<br />\n<a href=\"http://store.steampowered.com/app/395180/?l=polish\" rel=\"nofollow\">http://store.steampowered.com/app/395180/?l=polish</a>",
    "vote_count" => 0,
    "comments" => [],
    "receiver" => ${\$VAR1->[0]{"violation_url"}},
    "comment_count" => 0,
    "author_avatar_lo" => "http://xd.cdn02.imgwykop.pl/c3397992/avatar_def,q30.png",
    "author_avatar" => "http://xd.cdn02.imgwykop.pl/c3397992/avatar_def,q60.png",
    "receiver_avatar_lo" => ${\$VAR1->[0]{"violation_url"}},
    "receiver_avatar" => ${\$VAR1->[0]{"violation_url"}},
    "can_comment" => ${\$VAR1->[0]{"violation_url"}},
    "receiver_avatar_big" => ${\$VAR1->[0]{"violation_url"}},
    "user_vote" => 0,
    "embed" => {
      "plus18" => ${\$VAR1->[0]{"deleted"}},
      "source" => "https://www.youtube.com/watch?v=RunKiT2svRM",
      "url" => "https://www.youtube.com/watch?v=RunKiT2svRM",
      "preview" => "http://x3.cdn03.imgwykop.pl/c3201142/comment_IPKS5UCdZFSBJ6CN6vPt72s6ECWSG1a2,w400.jpg",
      "type" => "video"
    },
    "user_favorite" => ${\$VAR1->[0]{"deleted"}},
    "blocked" => ${\$VAR1->[0]{"deleted"}},
    "type" => "entry",
    "author_group" => 1,
    "author" => "Pawci0o"
  },
  {
    "app" => "iOS",
    "url" => "http://www.wykop.pl/wpis/18039173/jak-wymawiac-polskie-nazwiska-heheszki-euro2016/",
    "author_avatar_big" => "http://xT.cdn02.imgwykop.pl/c3397992/kostner_TCu1e2YjKR,q150.jpg",
    "violation_url" => ${\$VAR1->[0]{"violation_url"}},
    "author_avatar_med" => "http://xT.cdn02.imgwykop.pl/c3397992/kostner_TCu1e2YjKR,q48.jpg",
    "id" => 18039173,
    "deleted" => ${\$VAR1->[0]{"deleted"}},
    "receiver_group" => ${\$VAR1->[0]{"violation_url"}},
    "receiver_sex" => ${\$VAR1->[0]{"violation_url"}},
    ...