You can take screenshots in real time from PHP using the Browshot service. Get a free account and try now.
Update: version 1.10.2 uses cURL.
The source code is available on gitbub. You can download version 1.10.2 from our website.
Requirements: PHP 5.1.6 or above, PHPUnit 3.3.5 or above (to run unit tests)
Here are a couple of simple code samples.
require_once 'Browshot.php';
$browshot = new Browshot('my_key');
$info = $browshot->simple_file('/tmp/google.png', array('url' => 'http://www.google.com/'));
if ($info['code'] != "200") {
echo "Screenshot failed\n";
}
else {
echo "Screenshot saved at $file\n";
}
require_once 'Browshot.php';
$browshot = new Browshot('my_key');
$screenshot = $browshot->screenshot_create(array('url' => 'http://www.google.com/'));
# wait until screenshot is done
sleep(5);
$status = $browshot->screenshot_info($screenshot->{'id'});
while ($status->{'status'} != 'finished' && $status->{'status'} != 'error') {
sleep(1);
$status = $browshot->screenshot_info($screenshot->{'id'});
}
# screenshot failed?
if ($status->{'status'} == 'error') {
echo "Screenshot failed\n";
}
else {
$browshot->screenshot_thumbnail_file('/tmp/google.png', array('url' => $status->{'screenshot_url'})); # no width/height/scale mentioned, full screenshot retrieved
echo "Screenshot was saved to /tmp/google.png\n";
}
require_once 'Browshot.php';
$browshot = new Browshot('my_key');
$info = $browshot->simple_file('/tmp/google.png', array('url' => 'http://www.google.com/', 'width' => 640, 'height' => 480));
if ($info['code'] != "200") {
echo "Screenshot failed\n";
}
else {
echo "Screenshot saved at $file\n";
}
require_once 'Browshot.php';
$browshot = new Browshot('my_key');
$screenshot = $browshot->screenshot_create(array('url' => 'http://www.google.com/'));
# wait until screenshot is done
sleep(5);
$status = $browshot->screenshot_info($screenshot->{'id'});
while ($status->{'status'} != 'finished' && $status->{'status'} != 'error') {
sleep(1);
$status = $browshot->screenshot_info($screenshot->{'id'});
}
# screenshot failed?
if ($status->{'status'} == 'error') {
echo "Screenshot failed\n";
}
else {
$browshot->screenshot_thumbnail_file('/tmp/google-640.png', array('url' => $status->{'screenshot_url'}, 'width' => 640, 'height' => 480));
echo "Screenshot was saved to /tmp/google-640.png\n";
}
require_once 'Browshot.php';
$browshot = new Browshot('my_key');
$urls = array(
'http://browshot.com/' => 0,
'http://mobilito.net/' => 0,
'http://foo' => 0,
...
) ;
foreach ($urls as $url => $id) {
$screenshot = $browshot->screenshot_create(array('url' => $url));
if ($screenshot->{'status'} != 'error') {
$urls[$url] = $screenshot->{'id'};
}
}
# wait until screenshots are done
sleep(100);
$wait = 1;
while($wait > 0) {
$wait = 0;
foreach ($urls as $url => $id) {
if ($id == 0)
continue;
$status = $browshot->screenshot_info($id);
if ($status->{'status'} != 'finished' && $status->{'status'} != 'error') {
$wait++;
echo "Still waiting for $url\n";
}
elseif ($status->{'status'} == 'finished') {
$browshot->screenshot_thumbnail_file("/tmp/$id.png", array('id' => $id)); # no width/height/scale mentioned, full screenshot retrieved
echo "Saved $url to /tmp/$id.png\n";
}
}
if ($wait > 0)
sleep(10);
}
require_once 'Browshot.php';
$browshot = new Browshot('my_key');
$urls = array(
'http://browshot.com/',
'http://mobilito.net/',
'http://foo'
...
);
foreach ($urls as $url) {
$browshot->screenshot_create(array('url' => $url));
}
# wait until screenshots are done
sleep(100);
$wait = 1;
while($wait > 0) {
$wait = 0;
$list = $browshot->screenshot_list(array('limit' => count($urls)));
foreach ($list as $id => $screenshot) {
$url = $screenshot->{'url'};
if ($screenshot->{'status'} != 'finished' && $screenshot->{'status'} != 'error') {
$wait++;
echo "Still waiting for $url\n";
}
elseif ($screenshot->{'status'} == 'finished') {
$browshot->screenshot_thumbnail_file("/tmp/$id.png", array('id' => $id)); # no width/height/scale mentioned, full screenshot retrieved
echo "Saved $url to /tmp/$id.png\n";
}
}
if ($wait > 0)
sleep(10);
}