Skip to content

Instantly share code, notes, and snippets.

@lablnet
Last active February 23, 2022 03:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lablnet/a43c1e04ce508c95e7beafb70a7a1459 to your computer and use it in GitHub Desktop.
Save lablnet/a43c1e04ce508c95e7beafb70a7a1459 to your computer and use it in GitHub Desktop.
<?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