You can take screenshots in real time from PHP using the Browshot service. Get a free account and try now.
The source code is available on GitHub. You can get the latest version on on Packagist or you can download version 1.16.1 from our website.
Requirements: PHP 5.1.6 or above, PHPUnit 3.3.5 or above (to run unit tests)
You can use composer to set up Browshot:
composer require browshot-php/browshot=dev-master
This will create a vendor folder with the Browshot library and its dependencies. In your PHP file, you cal then load the PHP library:
require 'vendor/autoload.php';
If you don't want to use Composer, you can include the Browshot library directly into your PHP file:
require_once 'Browshot.php';
Here are a couple of basic code samples. You can find more code samples for each API call on the API Documentation page.
<?php
#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################
require 'vendor/autoload.php'; # Composer
# require_once 'Browshot.php'; # or download Browshot.php
$browshot = new Browshot('my_api_key');
$data = $browshot->simple(array('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 ($data['code'] == 200) { # success
$fp = fopen("screenshot.png", 'w');
fwrite($fp, $data['image']);
fclose($fp);
echo "Screenshot saved to screenshot.png\n";
}
else {
echo "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
$info = $browshot->simple_file("mobilito.png", array('url' => 'http://mobilito.net/', 'cache' => 0, 'instance_id' => 65, 'screen_width' => 1280, 'screen_height' => 1024)); # no cache, premium browser
if ($info['file'] != '') {
echo sprintf("Screenshot saved to %s\n", $info['file']);
}
else {
echo "The screenshot failed\n";
}
?>
<?php
#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################
require 'vendor/autoload.php'; # Composer
# require_once 'Browshot.php'; # or download Browshot.php
$browshot = new Browshot('my_api_key');
$screenshot = $browshot->screenshot_create(array('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'} != 'finished' && $screenshot->{'status'} != 'error') {
echo "Wait...\n";
sleep(10);
$screenshot = $browshot->screenshot_info($screenshot->{'id'});
}
# screenshot is done: finished (successful) or error (failed)
if ($screenshot->{'status'} == 'error') {
echo "Screenshot failed: " . $screenshot->{'error'} . "\n"; # display the reason for the error
}
else { # request the thumbnail
$image = $browshot->screenshot_thumbnail($screenshot->{'id'});
# save the screenshot
$fp = fopen("browshot.png", 'w');
fwrite($fp, $image);
fclose($fp);
}
?>
<?php
#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################
require 'vendor/autoload.php'; # Composer
# require_once 'Browshot.php'; # or download Browshot.php
$browshot = new Browshot('my_api_key');
$info = $browshot->simple_file('google-640.png', array('url' => 'http://www.google.com/', 'width' => 640, 'height' => 480));
if ($info['file'] == '' || $info['code'] != 200) {
echo "Screenshot failed\n";
}
else {
echo sprintf("Thumbnail saved at %s\n", $info['file']);
}
?>
<?php
require 'vendor/autoload.php'; # Composer
# require_once 'Browshot.php'; # or download Browshot.php
$screenshot = $browshot->screenshot_create(array('url' => 'http://www.google.com/', 'instance_id' => 12, 'size' => 'screen')); # 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'} != 'finished' && $screenshot->{'status'} != 'error') {
echo "Wait...\n";
sleep(10);
$screenshot = $browshot->screenshot_info($screenshot->{'id'});
}
# screenshot is done: finished (successful) or error (failed)
if ($screenshot->{'status'} == 'error') {
echo "Screenshot failed: " . $screenshot->{'error'} . "\n"; # display the reason for the error
}
else { # request the thumbnail
$browshot->screenshot_thumbnail_file($screenshot->{'id'},'/tmp/google-640.png', array('width' => 640, 'height' => 480));
echo "Screenshot was saved to /tmp/google-640.png\n";
}
?>
no credit card required
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