Skip to content

Instantly share code, notes, and snippets.

@litonarefin
Forked from lablnet/Capture.php
Created March 11, 2021 06:31
Show Gist options
  • Save litonarefin/655a423cf593b184837e43a7db37f268 to your computer and use it in GitHub Desktop.
Save litonarefin/655a423cf593b184837e43a7db37f268 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