AllsWeb Blog
No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
Support
Knowledgebase
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
No Result
View All Result
AllsWeb White Logo
No Result
View All Result
Home PHP and MySQL

How to Capture Screenshot of Website from URL using PHP

Get Screenshot of Website from URL

Lakshika Mathur by Lakshika Mathur
December 19, 2019
Reading Time: 3 mins read
0
How to Capture Screenshot of Website from URL using PHP

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.

RELATED POSTS

What is Application Programming Interface (APIs)?

Like Dislike Rating System with jQuery, Ajax, and PHP

Star Rating System with jQuery, Ajax, PHP, and MySQL

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

 

Tags: Capture ScreenshotPHP
ShareTweetSendShareSharePinScan
Lakshika Mathur

Lakshika Mathur

Related Posts

What is Application Programming Interface (APIs), Types, and Importance.
PHP and MySQL

What is Application Programming Interface (APIs)?

January 29, 2022
61
Like Dislike Rating System with jQuery, Ajax, and PHP
jQuery & AJAX

Like Dislike Rating System with jQuery, Ajax, and PHP

January 6, 2020
739
Star Rating System with jQuery, Ajax, PHP, and MySQL
jQuery & AJAX

Star Rating System with jQuery, Ajax, PHP, and MySQL

January 6, 2020
162
How to Force Download File in PHP
PHP and MySQL

How to Force Download File in PHP

January 2, 2020
82
How to Connect to the Remote MySQL Database using PHP
PHP and MySQL

How to Connect to the Remote MySQL Database using PHP

January 1, 2020
28
How to Generate QR Code with PHP using Google Chart API
PHP and MySQL

How to Generate QR Code with PHP using Google Chart API

January 1, 2020
83
Next Post
Mobile Number Verification via OTP SMS using PHP

Mobile Number Verification via OTP SMS using PHP

Autocomplete Address Field using Google Maps JavaScript API with Places Library

Autocomplete Address Field using Google Maps JavaScript API with Places Library

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Comparison (3)
  • HTML & CSS (9)
  • Interesting Facts (1)
  • JavaScript (27)
    • jQuery & AJAX (18)
  • PHP and MySQL (48)
  • Security (10)
  • SEO (2)
  • Trademark (2)
  • Tutorials (5)
  • Uncategorized (1)
  • Web Hosting (19)
    • VPS Server (5)
  • WordPress (8)

Recent Posts

  • Is the Trademark valuable to your Brand or domain?
  • Ideas For Ten Fantastic Online Business From Home
  • Some best free WordPress Themes for Affiliate Marketing Websites
  • Home
  • Posts
  • Privacy Policy
  • Terms and Conditions

Built and Maintained With ♥ by AllsWeb Team

No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison

Built and Maintained With ♥ by AllsWeb Team

Go to mobile version