viernes, 12 de septiembre de 2014

GrabzIt PHP Library: Tomar capturas de pantalla de páginas Web con la API GrabzIt


GrabzIt PHP Library es una clase PHP que te permite tomar capturas de pantalla de páginas Web usando GrabzIt y devolverlos como imágenes, tablas o documentos PDF.
 
Puede enviar una solicitud HTTP al servidor Web GrabzIt a través de la API GrabzIt para solicitar que el servicio tiene una captura de pantalla de una página determinada.
 
Cuando se envía la imagen que se está solicitando, varias opciones pueden configurarse como el tamaño de la ventana del navegador, el formato de la imagen resultante, si se debe utilizar la versión móvil de una página web, si sólo una parte de la página web debe ser capturada, e incluye una función callback para cuando la imagen de la página está lista. 
 
Descargar: GrabzIt PHP Library
 
Es requisito para que funcione descargar la API GrabzIt aquí

 Es importante configurar correctamente la API en config.php ...


 <?php
//Get this value from http://grabz.it/api
$grabzItApplicationKey = "APPLICATION KEY";
//Get this value from http://grabz.it/api
$grabzItApplicationSecret = "APPLICATION SECRET";
//The absolute path that you have placed the handler.php on your website
$grabzItHandlerUrl = "URL OF YOUR GrabzItHandler.php FILE (http://www.example.com/grabzit/handler.php)";
?> 


También viene con un script de demostración en index.php

<?php
include("lib/GrabzItClient.class.php");
include("config.php");

$message = '';

if (count($_POST) > 0)
{
        if (isset($_POST["delete"]) && $_POST["delete"] == 1)
        {
            $files = glob('results/*');
            foreach($files as $file)
            {
                if(is_file($file))
                    unlink($file);
            }
        }
        else
        {
        $url = $_POST["url"];
        $format = $_POST["format"];
        try
        {
            $grabzIt = new GrabzItClient($grabzItApplicationKey, $grabzItApplicationSecret);
            if ($format == "pdf")
            {
                $grabzIt->SetPDFOptions($url);
            }
            else
            {
                $grabzIt->SetImageOptions($url);
            }
            $grabzIt->Save($grabzItHandlerUrl);
        }
        catch (Exception $e)
        {
            $message =  $e->getMessage();
        }
    }
}
?>
<html>
<head>
<title>GrabzIt Demo</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="ajax/ui.js"></script>
</head>
<body>
<h1>GrabzIt Demo</h1>
<form method="post" action="index.php" class="inputForms">
<p>Enter the URL of the website you want to take a screenshot of. Then resulting screenshot should be saved in the <a href="results/" target="_blank">results directory</a>. It may take a few seconds for it to appear! If nothing is happening check the <a href="http://grabz.it/account/diagnostics" target="_blank">diagnostics panel</a> to see if there is an error.</p>
<?php
if ($grabzItHandlerUrl == "URL OF YOUR handler.php FILE (http://www.example.com/grabzit/handler.php)")
{
        ?><p><span class="error">You must set your call back to a valid public URL.</span></p><?php
}
if (!is_writable("results"))
{
    ?><span class="error">The "results" directory is not writeable! This directory needs to be made writeable in order for this demo to work.</span><?php
    return;
}
if (count($_POST) > 0 && !isset($_POST["delete"]))
{
    if (!empty($message))
    {
        ?><p><span class="error"><?php echo $message; ?></span></p><?php
    }
    else
    {
        ?><p><span style="color:green;font-weight:bold;">Processing screenshot.</span></p><?php
    }
}
?>
<label style="font-weight:bold;margin-right:1em;">URL </label><input text="input" name="url"/> <select name="format">
  <option value="jpg">JPG</option>
  <option value="pdf">PDF</option>
</select>
<input type="submit" value="Grabz It"></input>
</form>
<form method="post" action="index.php" class="inputForms">
<input type="hidden" name="delete" value="1"></input>
<input type="submit" value="Clear Results"></input>
</form>
    <br />
    <h2>Completed Screenshots</h2>
    <div id="divResults"></div>
</body>
</html>


No hay comentarios: