How to capture screenshot of website from URL in PHP



In this tutorial, I will show you how to capture screenshot of any website from URL in PHP. There are so many third party APIs are available to capture screenshot of the website using PHP script.

In the following example, I am using Google API to capture screenshot from given url. You can easily capture the screenshot using Google APIs.

There are many reason to capture screenshot of website to report any issues of the website to admin. To use Google API provided URL must be a valid url with “http”.

function getWebsiteScreenshot($url){

	$api_response = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=$url&screenshot=true");

	//decode json data
	$result = json_decode($api_response, true);

	//screenshot data
	$screenshot = $result['screenshot']['data'];
	$screenshot = str_replace(array('_','-'),array('/','+'),$screenshot);

	return $screenshot;
}

Now we need a URL of the website and call the getWebsiteScreenshot() to get the screenshot.


<form action="" method="post" name="myform">
	<span>Enter Website URL: <input type="url" size="60" required name="url">
	</span>
	<input type="submit" name="submit" value="Get Screenshot">
</form>


<?php if(!empty($_POST['url'])){ $url = $_POST['url']; ?>
	<img width="400px" src="data:image/jpeg;base64,<?php echo getWebsiteScreenshot($url); ?> " />
	<?php } ?>

Live Demo