<?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 list 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')); # Get full size thumbnails
if ($batch->{'status'} == 'error') {
	echo sprintf("Batch failed: %s\n", $batch->{'error'});
	exit(0);
}

echo 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);
}

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

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

?>