WebService::Browshot

You can install WebService::Browshot from CPAN. The source code is available on gitbub.

Here are a couple of basic code samples. You can find more code samples for each API call on the API Documentation page.

Download a screenshot (simple API)

#!/usr/bin/perl -w

use strict;
use warnings;
use Carp;

use WebService::Browshot;

#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################


my $browshot = WebService::Browshot->new(
	key	=> 'my_api_key',
	debug	=> 1, # print more debug information
);

my ($code, $png) = $browshot->simple(url => 'http://mobilito.net/', cache => 60 * 60 * 24 * 365, instance_id => 12); # 1 year cache, free screenshot
# code above is blocking, it will return when the screenshot finished or failed

if ($code == 200) { # sucess
	open PNG, "> screenshot.png" or croak "Could not open screenshot.png: $!\n";
	binmode PNG;
	print PNG $png;
	close PNG;
	
	print "Screenshot saved to screenshot.png\n";
}
else {
	print "Screenshot failed!\n";
	# the reason for the error is sent as part of the HTTP response in the X-Error header but it is not exposed by this library
}

# quicker way to save a screenshot
my ($new_code, $file) = $browshot->simple_file(url => 'http://mobilito.net/', file => "mobilito.png", cache => 0, instance_id => 65, screen_width => 1280, screen_height => 1024); # no cache, premium browser
if ($file ne '') {
	print "Screenshot saved to $file\n";
}
else {
	print "The screenshot failed\n";
}

Download code sample

Download a screenshot (full API)

#!/usr/bin/perl -w

use strict;
use warnings;
use Carp;

use WebService::Browshot;



my $browshot = WebService::Browshot->new(
	key	=> 'my_api_key',
	debug	=> 0, # no more debug information
);

my $screenshot = $browshot->screenshot_create(url => 'http://www.google.com/', instance_id => 12, size => 'page'); # all default parameters, instance_id = 12 (free)
# If the screenshot is already in cache, it could be finished already. Otherwise, wait longer
while ($screenshot->{status} ne 'finished' &&  $screenshot->{status} ne 'error') {
	print "Wait...\n";
	sleep 10;
	$screenshot = $browshot->screenshot_info(id => $screenshot->{id});
}

# screenshot is done: finished (sucessful) or error (failed)
if ($screenshot->{status} eq 'error') {
	print "Screenshot failed: ", $screenshot->{error}, "\n"; # display the reason for the error
}
else { # request the thumbnail
	my $image = $browshot->screenshot_thumbnail(id => $screenshot->{id});
	
	# save the screenshot
	open PNG, "> browshot.png" or croak "Cannot open browshot.png: $!\n";
	binmode PNG;
	print PNG $image;
	close PNG;
}

Download code sample

Download a 640x480 thumbnail (simple API)

#!/usr/bin/perl -w

use strict;
use warnings;
use Carp;

use WebService::Browshot;

#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################


my $browshot = WebService::Browshot->new(
	key	=> 'my_api_key',
	debug	=> 1, # print more debug information
);

my ($code, $file) = $browshot->simple_file(url => 'http://www.google.com/', file => '/tmp/google-640.png', width => 640, height => 480);

if ($file eq '' || $code != 200) {
  print "Screenshot failed\n";
}
else {
  print "Thumbnail saved at $file\n";
}

Download code sample

Download a 640x480 thumbnail (full API)

#!/usr/bin/perl -w

use strict;
use warnings;
use Carp;

use WebService::Browshot;

my $browshot = WebService::Browshot->new(
	key	=> 'my_api_key',
	debug	=> 1, # print more debug information
);

my $screenshot = $browshot->screenshot_create(url => 'http://www.google.com/', instance_id => 12, size => 'screen'); # all default paramters, instance_id = 12 (free)

# If the screenshot is already in cache, it could be finished already. Otherwise, wait longer
while ($screenshot->{status} ne 'finished' &&  $screenshot->{status} ne 'error') {
	print "Wait...\n";
	sleep 10;
	$screenshot = $browshot->screenshot_info(id => $screenshot->{id});
}

# screenshot is done: finished (sucessful) or error (failed)
if ($screenshot->{status} eq 'error') {
	print "Screenshot failed: ", $screenshot->{error}, "\n"; # display the reason for the error
}
else { # request the thumbnail
	$browshot->screenshot_thumbnail_file(id => $screenshot->{id}, file => '/tmp/google-640.png', width => 640, height => 480);
	print "Screenshot was saved to /tmp/google-640.png\n";
}

Download code sample

Try it for free

close

sign up for free account

Already have an account?

no credit card required

About Us

Browshot is a web service to create real time web screenshots in a multitude of virtual devices, including mobile devices like the iPhone 3 & 4, iPad, Android Nexus, etc.

You can use the web dashboard, or our full-featured API.

  • Real time screenshots

  • 15+ mobile devices: iPhone, iPad, Android, etc.

  • 30+ desktop resolutions

  • Fast and reliable

  • Thumbnails of any size, any ratio

  • Full API, open-source libraries

Share