<?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



$screenshot = $browshot->screenshot_create(array('url' => 'https://browshot.com/', 'details' => 3, 'instance' => 24, 'screen_width' => 1280, 'screen_height' => 1024)); # extra details
# the call to screenshot/create sends the same response as screenshot/information
# so screenshot/create and screenshot/info can be interchanged as long as the cache value is long enough
while ($screenshot->{'status'} != 'finished' &&  $screenshot->{'status'} != 'error') {
	echo "Wait...\n";
	sleep(10);
	$screenshot = $browshot->screenshot_info($screenshot->{'id'}, array('details' => 3));
	# or $screenshot = $browshot->screenshot_create(array('url' => 'https://browshot.com/', 'details' => 3, 'instance' => 24, 'screen_width' => 1280, 'screen_height' => 1024));
}

# 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
	
	# If you call $browshot->screenshot_create with the same parameters, a new screenshot will be requested.
}
else { # screenshot is finished
	# did the page load (200 OK) or was mnissing (404 Not Found) or some error (40X, 50X);
	if ($screenshot->{'response_code'} != 200) {
		echo sprintf("The page may not have be reached, HTTP response code was: %s\n", $screenshot->{'response_code'});
	}
	
	# Was it an HTML page, PDF or image?
	if ($screenshot->{'content_type'} != 'text/html') {
		echo sprintf("The page is not HTML, it is %s\n", $screenshot->{'content_type'});
	}
	
	# with details = 3, you can check what asserts were loaded on the page: images, javascript, frames, etc.
	echo sprintf("There are %d images on this page:\n", count($screenshot->{'images'}));
	foreach ($screenshot->{'images'} as $url) {
		echo "$url\n";
	}
}
