Last active
February 23, 2022 03:25
-
-
Save lablnet/a43c1e04ce508c95e7beafb70a7a1459 to your computer and use it in GitHub Desktop.
Capture the screenshot of Url using php https://medium.com/@lablnet/capture-screenshot-of-website-from-url-using-php-2f25487c068
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Capture | |
{ | |
/** | |
* Capture web screenshot using google api. | |
* | |
* @param (string) $url Valid url | |
* | |
* @return blob | |
*/ | |
public function snap($url) | |
{ | |
//Url value should not empty and validate url | |
if (!empty($url) && filter_var($url, FILTER_VALIDATE_URL)) { | |
$curl_init = curl_init("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url={$url}&screenshot=true"); | |
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, true); | |
$response = curl_exec($curl_init); | |
curl_close($curl_init); | |
//call Google PageSpeed Insights API | |
//decode json data | |
$googlepsdata = json_decode($response, true); | |
//screenshot data | |
$snap = $googlepsdata['screenshot']['data']; | |
$snap = str_replace(['_', '-'], ['/', '+'], $snap); | |
return $snap; | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment