Skip to content

Instantly share code, notes, and snippets.

@aabir
Last active August 29, 2015 14:13
Show Gist options
  • Save aabir/9dc5c816b60ceec956f3 to your computer and use it in GitHub Desktop.
Save aabir/9dc5c816b60ceec956f3 to your computer and use it in GitHub Desktop.
Grab website screen shot using API
<?php
$url = 'https://sender.blockspring.com/api_v2/blocks/07b6410853fa5343076db5d742de58e7?api_key=you_api_key';
$data = json_encode(array("url" => "http://www.facebook.com/", 'width' => 1200, 'height' => 1000));
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => array("Accept: application/json", "Content-Type: application/json"),
'method' => 'POST',
'content' => $data,
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
//var_dump(json_decode($result));
$image = (json_decode($result, true));
$image = $image['screenshot']['data'];
$image = str_replace(array('_','-'),array('/','+'),$image);
echo "<img src=\"data:image/png;base64,".$image."\" border='1' />";
?>
<?php
$url = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://www.stackoverflow.com&screenshot=true';
$result = file_get_contents($url);
//var_dump(json_decode($result));
$var = (json_decode($result, true));
$image = $var['screenshot']['data'];
$image = str_replace(array('_','-'),array('/','+'),$image);
echo "<img src=\"data:image/jpeg;base64,".$image."\" border='1' />";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment