-
-
Save litonarefin/655a423cf593b184837e43a7db37f268 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