<?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', 1); # more debug information


$list = $browshot->screenshot_multiple(array('http://www.google.com/', 'https://browshot.com/', 'http://mobilito.net/'), # up to 5 URLs
	array(12, 24, 72), # up to 10
	array('size' => 'page') # all options of screenshot/create are valid
);


$ids = array(); # will hold all screenshot IDs

foreach ($list as $id => $screenshot) {
	array_push($ids, $id);
}

sleep(20);


while (count($ids) > 0) { # go through each screenshot
	$i = 0;
	while ($i < count($ids)) {
		$id = $ids[$i];

		$info = $browshot->screenshot_info($id);
		if ($info->{'status'} == 'finished') {
			$browshot->screenshot_thumbnail_file($id, "$id.png");
			array_splice($ids, $i, 1); # remove ID from the list
		}
		elseif ($info->{'status'} == 'error') {
			echo sprintf("Screenshot failed: %s\n", $info->{'error'});
			echo printf("\tURL: %s\n", $info->{'url'});
			echo printf("\tinstance_id: %s\n", $info->{'instance_id'});
			echo "\n";
			
			array_splice($ids, $i, 1); # remove ID from the list
		}
		else {
			# wait
			$i++;
		}
	}
	
	if (count($ids) > 0)
		sleep(10);
}

?>