Saturday, September 16, 2017

PHP Script to Capture Screenshot of Website from URL

PHP Script to Capture Screenshot of Website from URL

In this article you will learn about 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.

Also you can use this script depending upon your requirement. Mostly you have seen this script in various google product, if you remember while sending the bug report it is automatically capture the web page screenshot.

Lets follow the below steps to capture screenshot of webpage :

Screenshot.php

<?php
   //enter your website url
   $siteURL = "http://www.skptricks.tk/";
   
   //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."\" />";
   
   ?>

Output :
----------------
PHP Script to Capture Screenshot of Website from URL




1 comment:

  1. It seems to me the easiest way to create a screenshot is to use this function
    function SiteScreenshot($token, $url, $width, $height, $response_type = 'image') {
    // Parameters.
    $token = $token; ///YOUR_API_TOKEN
    $url = urlencode($url);
    $width = $width;
    $height = $height;
    $response_type = $response_type; ///Response type: json, image

    // Create the query URL.
    $query = "https://api.pikwy.com/";
    $query .= "?token=$token&url=$url&width=$width&height=$height&response_type=$response_type";

    // Call the API.
    $image = file_get_contents($query);

    // Store the screenshot image.
    file_put_contents('./screenshot.png', $image);
    }

    ReplyDelete