In this article you will learn “How to Capture website Screenshot through website URL using Google PageSpeed Insights API and PHP”
Usually, Google PageSpeed Insight API is used to check the performance and speed of any website or webpage. But you never used Google PageSpeed Insights API to get website screenshot using URL. Website page screenshot capture functionality is used for many purposes in the web application.
Now you can also use Google PageSpeed Insights API to capture a screenshot of webpage from URL. There are various third party APIs available to capture a screenshot of the website. But if you want to build your own script to capture a screenshot from URL. With the help of Google PageSpeed Insights API and PHP You can do it easilly.
Follow the below example script to takes a screenshot of the website by URL:
To create web page snapshot, Google PageSpeed Insights API need to be called using following params.
url: Specify the URL of webpage or website
Screenshot: Specify screenshot=true to retrieve the screenshot data.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/website url $siteURL = "http://www.tutorialswebsite.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 capture screenshot image echo "<img src=\"data:image/jpeg;base64,".$screenshot."\" />"; |
Get Website Screenshot from URL
In this example, we will show you “how you can build a form to capture website screenshot by the URL provided by the user and display the page screenshot to user”.
HTML Code:
Following HTML form create a input field which is used to enter website URL.
2 3 4 5 6 7 8 9 |
<form method="post" action="capturescreenshot.php" > <p>Website URL: <input type="text" name="url" value="" /></p> <input type="submit" name="submit" value="CAPTURE"> </form> |
capturescreenshot.php
Following PHP code is used to validate the submitted input URL value before calling the Google PageSpeed Insights API. The Screenshot data retrieved from the Google PageSpeed Insights API and image of the website screenshot is shown.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?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."; } } ?> |
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
[sociallocker]
[/sociallocker]
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co