PHP Libraries Browshot


Browshot

You can take screenshots in real time from PHP using the Browshot service. Get a free account and try now.

Browshot.php

The source code is available on gitbub. You can download version 1.14.0 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.

Download a screenshot (simple API)

require_once 'Browshot.php';

$browshot = new Browshot('my_key');
$info = $browshot->simple_file('/tmp/google.png', array('url' => 'http://www.google.com/', 'instance_id' => 65));

if ($info['code'] != "200") {
  echo "Screenshot failed\n";
}
else {
  echo "Screenshot saved at $file\n";
}

Request a screenshot (full API)

require_once 'Browshot.php';

$browshot = new Browshot('my_key');
$screenshot = $browshot->screenshot_create(array('url' => 'http://www.google.com/', 'instance_id' => 65));

# 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($screenshot->{'id'}, '/tmp/google.png'); # no width/height/scale mentioned, full screenshot retrieved
  echo "Screenshot was saved to /tmp/google.png\n";
}


Download a 640x480 thumbnail (simple API)

require_once 'Browshot.php';

$browshot = new Browshot('my_key');
$info = $browshot->simple_file('/tmp/google.png', array('url' => 'http://www.google.com/', 'instance_id' => 65, 'width' => 640, 'height' => 480));

if ($info['code'] != "200") {
  echo "Screenshot failed\n";
}
else {
  echo "Screenshot saved at $file\n";
}

Download a 640x480 thumbnail (full API)

require_once 'Browshot.php';

$browshot = new Browshot('my_key');
$screenshot = $browshot->screenshot_create(array('url' => 'http://www.google.com/', 'instance_id' => 65));


# 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($screenshot->{'id'}, '/tmp/google-640.png', array('width' => 640, 'height' => 480));
  echo "Screenshot was saved to /tmp/google-640.png\n";
}

Bulk submissions

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, 'instance_id' => 12));
  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($id, "/tmp/$id.png"); # no width/height/scale mentioned, full screenshot retrieved
      echo "Saved $url to /tmp/$id.png\n";
    }
  }
  
  if ($wait > 0)
    sleep(10);
}


Bulk submissions (using screenshot_list)

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, 'instance_id' => 22));
}

# 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($id, "/tmp/$id.png"); # no width/height/scale mentioned, full screenshot retrieved
      echo "Saved $url to /tmp/$id.png\n";
    }

  }
  
  if ($wait > 0)
    sleep(10);
}

close

Sign up for a free account