You can use the web page screenshot capture functions for numerous purposes in the web application. So, there are many third-party APIs that are accessible to take a screenshot of the website. But, if you wish to build your script to get a screenshot from URL, you can do it effortlessly by using PHP and Google PageSpeed Insights API.
Usually, Google PageSpeed Insights API is used to measure the performance of a web page. Therefore, you can also use Google PageSpeed Insights API to get a screenshot of the website from the URL. So, in this tutorial, we will show you how to capture a screenshot of the site from the URL with the help of Google PageSpeed Insights API and PHP.
The following example script takes a screenshot of the website by the URL and shows it as an image.
Get Screenshot of Website from URL
To make a web page snapshot, Firstly, you have to call Google PageSpeed Insights API with the following params.
url:
Specify the URL of the website.screenshot:
stipulate screenshot=true to retrieve the screenshot data.
//website url $siteURL = "http://www.allsweb.com/"; //call Google PageSpeed Insights API $googlePagespeedData = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=$siteURL&screenshot=true"); //decode json data $googlePagespeedData = json_decode($googlePagespeedData, true); //screenshot data $screenshot = $googlePagespeedData['screenshot']['data']; $screenshot = str_replace(array('_','-'),array('/','+'),$screenshot); //display screenshot image echo "<img src=\"data:image/jpeg;base64,".$screenshot."\" />";
Capture Website Screenshot from URL
In this example script, we will explain how you can build a form to get a website screenshot by the URL given by the user and show the web page screenshot to the user.
HTML:
The HTML form below contains an input field that accepts the website’s URL. On the form, submit a site URL for the PHP script to take a screenshot of the website.
<form method="post" action="getScreenshot.php" > <p>Website URL: <input type="text" name="url" value="" /></p> <input type="submit" name="submit" value="CAPTURE"> </form>
PHP (getScreenshot.php):
Before calling the call Google PageSpeed Insights API, Validate the submitted input value to check whether it is a valid URL. Retrieve the screenshot data from the Google PageSpeed Insights API, and show the screenshot image of the website.
<?php if(!empty($_POST['url'])){ //website url $siteURL = $_POST['url']; if(filter_var($siteURL, FILTER_VALIDATE_URL)){ //call Google PageSpeed Insights API $googlePagespeedData = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=$siteURL&screenshot=true"); //decode json data $googlePagespeedData = json_decode($googlePagespeedData, true); //screenshot data $screenshot = $googlePagespeedData['screenshot']['data']; $screenshot = str_replace(array('_','-'),array('/','+'),$screenshot); //display screenshot image echo "<img src=\"data:image/jpeg;base64,".$screenshot."\" />"; }else{ echo "Please enter a valid URL."; } } ?>
Also, read our previous blog- Pagination in PHP with MySQL