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

$screenshot = $browshot->screenshot_create(array('url' => 'http://www.google.com/', 'instance_id' => 12, 'size' => 'page')); # all default parameters, instance_id = 12 (free)
# If the screenshot is already in cache, it could be finished already. Otherwise, wait longer
while ($screenshot->{'status'} != 'finished' &&  $screenshot->{'status'} != 'error') {
	echo "Wait...\n";
	sleep(10);
	$screenshot = $browshot->screenshot_info($screenshot->{'id'});
}

# screenshot is done: finished (sucessful) or error (failed)
if ($screenshot->{'status'} == 'error') {
	echo sprintf("Screenshot failed: %s\n", $screenshot->{'error'}); # display the reason for the error
}
else { # request the thumbnail
	$image = $browshot->screenshot_thumbnail($screenshot->{'id'});
	
	# save the screenshot
	$fp = fopen("browshot.png", 'w');
	fwrite($fp, $image);
	fclose($fp);
	
	echo "Screenshot saved to browshot.png\n";
}


# you can combine several calls into 1: screenshot_create + screenshot_host
$screenshot = $browshot->screenshot_create(array(
	'url' 			=> 'https://browshot.com/', 
	'instance_id' 	=> 24, # premium
	'screen_width' => 1600, # wide screen
	'screen_height' => 1200, 
	'size' => 'page', # full page
	'delay' => 25, # greater delay if the page si very longer
	'hosting' => 's3', # make sure you have set up your S3 bucket with the right ACL
	'hosting_bucket' => 'my_bucket',
	'hosting_file' => 'myfile.png', # can be omitted
	'hosting_width' => 400, # thumbnail 400px wide
));


# Make multiple screenshots of the same page (loaded once)
$screenshot = $browshot->screenshot_create(array(
	'url' 			=> 'https://youtube.com/', 
	'instance_id' 	=> 24, # premium
	'shots' => 5 ,          # 5 screenshots
	'shot_interval' => 10, # every 10 seconds
	'screen_width' => 1280,
	'screen_height' => 1024,
));

sleep(60);

$screenshot = $browshot->screenshot_info($screenshot->{'id'});
if ($screenshot->{'status'} == 'finished') {
	# download the 5 screenshots
	for ($i = 1; $i <= 5; $i++) {
		$browshot->screenshot_thumbnail_file($screenshot->{'id'}, "$i.png", array('height' => 600, 'shot' => $i));
	}
}


# Host the screenshot on S3
$screenshot = $browshot->screenshot_create(array(
	'url' => 'https://youtube.com/', 
	'instance_id' => 24, # premium
	'screen_width' => 1280,
	'screen_height' => 1024,
	'hosting' => 's3',
	'hosting_bucket' => 'my_bucket',
	'hosting_file' => 'youtube.png',
	'hosting_width' => 600 # thumbnail
));

sleep(60);

$screenshot = $browshot->screenshot_info($screenshot->{'id'});
if ($screenshot->{'status'} == 'finished') {
	# The screenshot may not be hosted yet
	if (array_key_exists('hosted_url', $screenshot) && $screenshot->{'hosted_url'} != '') {
		echo sprintf("\nScreenshot hosted at %s\n", $screenshot->{'hosted_url'});
	}
}
?>