How to Capture Screenshot of Website from URL using PHP


Web page screenshot capture functionality is used for various purposes in the web application. There are many third party APIs are available to take a screenshot of the website. But if you wish to build your own script to get a screenshot from URL, you can do it easily using PHP and Google PageSpeed Insights API.

Generally, Google PageSpeed Insights API is used to measure the performance of a web page. But you can also use Google PageSpeed Insights API to get a screenshot of the website from URL. In this tutorial, we will show you how to capture a screenshot of the website from URL using Google PageSpeed Insights API and PHP.

The following example script takes a screenshot of the website by the URL and shows as an image.

Get Screenshot of Website from URL

To make web page snapshot, Google PageSpeed Insights API need to be called with the following params.

  • url: specify the URL of the website.
  • screenshot: specify screenshot=true to retrieve the screenshot data.
//website url
$siteURL "http://www.codexworld.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($googlePagespeedDatatrue);

//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 show how you can build a form to get website screenshot by the URL provided by the user and display the web page screenshot to the user.

HTML:
The following HTML form has one input field which accepts URL of the website. On form submit the site URL is submitted to the PHP script to get the 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, the submitted input value is validated to check whether it is a valid URL. The screenshot data is retrieved from the Google PageSpeed Insights API and screenshot image of the website is shown.

<?php
if(!empty($_POST['url'])){
    
//website url
    
$siteURL $_POST['url'];
    if(
filter_var($siteURLFILTER_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($googlePagespeedDatatrue);
        
//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.";
    }
}
?>

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Recommended Tutorials For You

3 Comments

  1. Damith Said...
  2. Sagar Nandwani Said...
  3. Rajkishor Said...

Leave a reply