How to Capture Screenshot of a Website Using PHP
Capturing website using PHP script is very easy. In this post, explain you how to capture screenshot using PHP code. We are using an PHP class “screenshot.class.php”. It will send an web request to ScreenshotLayer API to grab the website screenshot in JPG, GIF or PNG formats.
Steps to Capture Website Screenshot Using PHP Script

Get ScreenshotLayer API key:
Now you need the api key to access this class code. Using this api key an HTTP request send to screenshotLayer service to capture the screen.
To get api key SignUp here: https://screenshotlayer.com/product
Use ScreenShotLayer Php Class code:
Use below screenshot.class.php code. Just use you own API-KEY in this file and include this file in your screen capture sample.php file.
<?php
class screenShot{
//*********************************************************
// Settings
//*********************************************************
//Your screenshotLayer API key
//Available at https://screenshotlayer.com/product
private $apiKey = 'YOUR_API_KEY_HERE';
//API endpoint
//only needs to change if the API changes location
private $endPoint = 'http://api.screenshotlayer.com/api/capture';
//Secret Keyword defined in your screenshotLayer dashboard
//leave blank if you have not activated this feature
private $secretKey = '';
//API key/value pair params
public $params = array();
//response capture
public $capture;
//image info
public $imageInfo;
/*
method: class construction
usage: screenShot([string url]);
params: url = URL to web video
The screenshotLayer API requires a valid webpage URL to capture.
returns: null
*/
public function __construct($url='',$format='PNG'){
$this->params['url'] = $url;
if( !empty($this->secretKey) ){
$secret = md5($url.$this->secretKey);
$this->params['secret_key'] = $secret;
}
}
/*
method: displayImage
usage: displayImage(void);
params: none
This method will write an image tag with the correct request url.
Note: this method will not verify the request is to a valid url and
will return a broken image if not.
returns: null
*/
public function displayImage(){
$request = $this->buildRequest();
echo '<img src="'.$request.'">';
}
/*
method: captureScreen
usage: captureScreen(void);
params: none
This method will query the api for the binary code of the captured webpage.
returns: null
*/
public function capturePage(){
$request = $this->buildRequest();
$this->capture = file_get_contents($request);
$this->imageInfo = getimagesizefromstring($this->capture);
if( empty($this->imageInfo) ){
if( empty($this->capture) ){
throw new Exception('An unknown error has occured');
}else{
$response = json_decode($this->capture);
throw new Exception($response->error->info);
}
}
}
/*
method: downloadCapture
usage: downloadCapture([string fileName='']);
params: fileName = The name of the file written to disk
This method will download the captured image to the client.
returns: null
*/
public function downloadCapture($fileName=''){
$fileName = ( empty($fileName) ) ? 'capture' : $fileName;
if( empty($this->capture) ){
throw new Exception('No image has been captured');
}
header('Content-Type: '.$this->imageInfo['mime']);
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('Content-Transfer-Encoding: binary');
echo $this->capture;
}
/*
method: displayCapture
usage: displayCapture(void);
params: none
This method will display the captured image to the browser.
returns: null
*/
public function displayCapture(){
if( empty($this->capture) ){
throw new Exception('No image has been captured');
}
header('Content-Type: '.$this->imageInfo['mime']);
echo $this->capture;
}
/*
method: buildRequest
usage: buildRequest(void);
params: none
This method will build the api request url.
returns: api request url
*/
public function buildRequest(){
if( empty($this->params['url']) ){
throw new Exception('API requires URL to video');
}
$request = $this->endPoint.'?access_key='.$this->apiKey;
foreach( $this->params as $key=>$value ){
if( $key == 'url' ){
$request .= '&url='.urlencode($value);
}else{
$request .= '&'.$key.'='.$value;
}
}
return $request;
}
/*
method: setParam
usage: setParam(string key, string value);
params: key = key of the params key/value pair
value = value of the params key/value pair
add or change the params key/value pair specified.
returns: null
*/
public function setParam($key,$value){
$this->params[$key] = $value;
}
}
?>Write PHP code to Capture Screen
You just write code for use this api call to take the website screenshot. Follow below “sample.php” file code for this.
<?php
//include screenShot class
include('screenshot.class.php');
//use own website url
$screenShot = new screenShot('http://www.example.com');
//display html image tag for captured webpage
$screenShot->displayImage();
//$screenShot->capturePage();
//$screenShot->downloadCapture('test.png');
//$screenShot->displayCapture();
?>Just get both files and upload it on website accessible location. Firstly image take some time capture. This api store your image for 30 days only untill you capture again.
You can also check complete ScreenshotLayer API documentation here: https://screenshotlayer.com/documentation. Implement any api method as per your need. Its quite easy to implement this PHP code to Capture Web Screenshot.

An interesting example, but you can just get by with one function, something like this:
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);
}