0

I have some urls daily , and need capture screenshot from these URLs. I want to save specific urls screenShot in my Host. Now I want to try it in localhost/wamp and also in linux host .

I try some libraries but weren`t successfully . I want to try with google api pagespeed v5 .

<?php
$url = 'mysite.com';
$response =     file_get_contents("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=$url&screenshot=true");
$googlePagespeedObject = json_decode($response, true);
$screenshot = $googlePagespeedObject['screenshot']['data'];
$screenshot = str_replace(array('_','-'), array('/','+'), $screenshot);
echo "<img src=\"data:image/jpeg;base64,{$screenshot}\"     alt=\"Screenshot\" />";
file_put_contents('...', base64_decode($screenshot));

my error without any successfully image :

Warning: file_get_contents(https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=mysite.com&screenshot=true): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in

Regolith
  • 2,944
  • 9
  • 33
  • 50
misagh ahmadi
  • 79
  • 1
  • 12
  • 1
    Possible duplicate of [Website screenshots](https://stackoverflow.com/questions/757675/website-screenshots) – Hackinet Jul 14 '19 at 19:31
  • the method doesnt work so I posted :-) – misagh ahmadi Jul 14 '19 at 20:07
  • https://developers.google.com/speed/docs/insights/v5/get-started "If you're just trying out the PageSpeed Insights API, you don't need an API key. If you plan on using the API in an automated way and making multiple queries per second, you'll need an API key." – ceejayoz Jul 15 '19 at 17:24
  • there's a problem with your connection or company proxy, try upload your file to hosting or server, it'll work. and don't forget to add http or https to your `$url = 'mysite.com';` – sancoLgates Jul 29 '19 at 09:41

2 Answers2

1

i modified your code a bit, Try this code. working example, saved image here

function ScreenShotUrl($url){
  $link = $url ;
  $name = parse_url($link)['host'];
  $resp = file_get_contents("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=$link&screenshot=true");
  $resp = json_decode($resp, true);
  $data = str_replace('_','/',$resp['lighthouseResult']['audits']['final-screenshot']['details']['data']);
  $data = str_replace('-','+',$data);
  echo '<img src="' . $data . '" />';
  $img = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data)); 
  file_put_contents('tmp/'.$name.'.jpg', $img);
} 
$url = 'http://bajukakilima.com'; 
ScreenShotUrl($url);
sancoLgates
  • 188
  • 12
-1

Here a Function to use for php screenshot :

function ScreenShotUrl($url){
$link = $url ;
$name = parse_url($link)['host'];
$googlePagespeedData = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=$link&screenshot=true");
$googlePagespeedData = json_decode($googlePagespeedData, true);
$screenshot = base64_decode($googlePagespeedData['screenshot']['data']);
$data = str_replace('_','/',$googlePagespeedData['screenshot']['data']);
$data = str_replace('-','+',$data);
$decoded = base64_decode($data);
file_put_contents('images/screenshots/'.$name.'.jpg',$decoded);
$file_name = "$name.jpg";
}
misagh ahmadi
  • 79
  • 1
  • 12