<?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');


# Create a text file with a lsit of URLs
$fp = fopen("batch.txt", 'w') or die("Unable to open batch.txt!");;
fwrite($fp, "google.com\n");
fwrite($fp, "mobilito.net\n");
fclose($fp);


$batch = $browshot->batch_create("batch.txt", array('instance_id' => 65, 'screen_width' => 1600, 
  'screen_height' => 1200, 'size' => 'page', 'width' => 600)); # Get thumbnails
  
if ($batch->{'status'} == 'error') {
	echo sprintf("Batch failed: %s\n", $batch->{'error'});
	exit(0);
}

sprintf("Batch #%d in process\n", $batch->{'id'});

sleep(30);
while ($batch->{'status'} != 'finished' && $batch->{'status'} != 'error') {
	sleep(15);
	$batch = $browshot->batch_info($batch->{'id'});
}

if ($batch->{'status'} == 'error') {
	echo sprintf("Batch failed: %s\n", $batch->{'error'});
	exit(0);
}

# Check how many screenshot failed
echo sprintf("%d/%d screenshots failed\n", $batch->{'failed'}, $batch->{'count'});

# The batch succeeded, download the archive. There may be fore than 1 URL
foreach ($batch->{'urls'} as $url) {
	echo "Downloading $url...\n";
	# Download the archive
}

if (count($batch->{'urls'}) > 1) {
	# Run 7a if the archive was split into multiple files
}

?>