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.
1 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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);
}