How to Take Website Screenshot using PHP

PHP makes it easy to do different jobs by using simple commands. If you are a PHP developer you will know that PHP does not require too much coding to do different things and it is not too lengthy code to take website screenshot using PHP you can see the code below copy it and run it in your localhost server to get a screenshot of any website.
simply create a new index.php file in paste the following code in it.
<?php
//website url
$siteURL = "http://www.softwebtuts.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."\" />";
?>
Replacements:
You simply have to replace the URL of the website of which you want to take a screenshot like in the above code you have to replace http://softwebtuts.blogspot.com the URL of the website of which you want to take a screenshot.
In the code provided above we have to use Google page speed insights API to capture screenshots of a website.
Conclusion:
That's it you can copy the code and use it to take screenshots of a website. I hope this article helped you a lot.
Be The First To Comment