Note: Detailed response codes list will be posted soon...
Website Screenshots API - Advanced Method
Pre-requisites
- PHP4 installed with GD 1.8 or later compiled
- Basic programming knowledge or problem-solving ability
- About ten (10) minutes to get it up-and-running with default configuration
Download a zip file containg the sample PHP4 website screenshot API code
Sample Website Thumbnail PHP4 Code
The following code is provided "as-is" with no guarantees or warranties implied. It can be used freely and can be inserted into an existing application (once <?php ?> tags removed) or can be included as a separate file. Its purpose is to define the functions that interact with the ShrinkTheWeb API directly. Do not share your "Secret_Access_Key" with anyone, as doing so could compromise your account security.
Note: By turning on the cache option, all screenshot requests will be downloaded to your server into the directory location you specify. This is all that is required to make images available to your custom application or project. To display the thumbnails (with or without caching enabled), please see the sample PHP4 code provided below this block.
<?php
/**
* ShrinkTheWeb Url Thumbnail Utility Package
*
* Version: 1.0.2
*/
function shrinktheweb($size, $url){
$access_key_id="[Access_Key_Id]"; // Replace [Access_Key_Id] with your actual Access_Key_Id
$secret_access_key="[Secret_Access_Key]"; // Replace [Secret_Access_Key] with your actual Secret_Access_Key
$default_image="http://www.neobus.net/images/thumbs/nothumb.jpg"; // Enter the default image you would like shown if no thumbnail
$cache=0; // Set to 1, if you want to save thumbnails locally (config function get_html_snippet also)
$url_enc = urlencode($url);
$request_url = "http://www.shrinktheweb.com/xino.php?"
. "Service=". "ShrinkWebUrlThumbnail"
. "&Action=". "Thumbnail"
. "&STWAccessKeyId=". $access_key_id
. "&Size=" . $size
. "&stwu=" . $secret_access_key
. "&stwUrl=" . $url;
$line=make_http_request($request_url);
$num_matches = preg_match('/<[^:]*:Thumbnail\\s*(?:Exists=\"((?:true)|(?:false))\")?[^>]*>([^<]*)<\//', $line, $matches);
if($num_matches == 1){
$exists = $matches[1];
$thumbnail = $matches[2];
}else{
$exists = NULL;
$thumbnail = NULL;
}
$has_default_image = ($default_image != NULL) && (strlen($default_image) > 0);
if ($thumbnail != NULL && ($exists || !$has_default_image)){
return get_html_snippet($url,$thumbnail,1,$exists,$cache);
}
return get_html_snippet($url,$default_image,1,$exists,$cache);
}
// Make an http request to the specified URL and return the result
function make_http_request($url){
$lines = file($url);
return implode("", $lines);
}
// Returns an HTML snippet which will display the thumbnail image url and link to the website.
// Returns an error code via XML and display error image or queue image upon failure
function get_html_snippet($url, $image, $stw, $exists, $cache) {
$homedir="[homedir]"; // Replace [homedir] with the path to store thumbnails (i.e. /home/username/public_html/cached/images/)
$link = "";
$navigable_url = (stristr($url,"http://") == $url ) ? $url : "http://".$url;
if ($image) {
if($stw==1) {$link = "<img border='0' src='$image' alt='$url'/>";}
else {$link = "<a href='$navigable_url'><img border='0' src='$image' alt='$url'/></a>";}
$nothumb=substr_count(strtolower($image),"nothumb");
if($exists=="true" && $cache==1) {
// Cache the thumbnail
$subdom="$homedir".substr($url,4).".jpg";
if(substr(strtolower($url),0,4)=="www.") {$dom=$subdom;}
else {$dom = "$homedir".$url.".jpg";}
// echo $subdom."(".iif(file_exists($subdom),"1","0")."),".$dom."(".iif(file_exists($dom),"1","0")."),".$stw."<br />";
if((!file_exists($dom) || !file_exists($subdom)) && $stw==1 && $nothumb==0)
{
// Store ShrinkTheWeb Thumbnails
$imagedata = @imagecreatefromjpeg($image); if($imagedata!='') {imagejpeg($imagedata,$dom,100);}
}
}
}
return $link;
}
?>
IMPORTANT: Remember to change the [Access_Key_Id] and [Secret_Access_Key] values in the URL to your access key ID and secret key, respectively. These can be found when you first login (in the lobby).
Sample WebSite Thumbnail PHP Code - Basic Operations
The following code is provided "as-is" with no guarantees or warranties implied. It can be used freely and can be inserted into an existing application (once <?php ?> tags removed) or can be included as a separate file. Its purpose is to provide a basic framework for displaying website screenshots in your PHP-enabled web pages.
<?php
/**
* ShrinkTheWeb Url Thumbnail Sample Code
*
* Version: 1.0.0
*/
$homedir="[homedir]"; // Replace [homedir] with the path to store thumbnails (i.e. /home/username/public_html/cached/images/)
// Calculate time to retrieve images (avoid STW hanging remote website, if STW is unresponsive)
list($usec,$sec)=explode(' ', microtime()); $get_begin=(float)$sec+(float)$usec;
if(@fsockopen("www.shrinktheweb.com",80, $errno, $errstr, 2.0)) {$stw=1;}else{$stw=0;}
list($usec,$sec)=explode(' ', microtime()); $get_end=(float)$sec+(float)$usec;
$timetoget=round($get_end-$get_begin, 5);
if($stw==1) {if($timetoget<=0.25) {$stw=1;} else {$stw=0;}} // Opened but was it fast enough?
// We need to pass $domain into the following...
unset($thumbnail); unset($thexists); $dom=""; $subdom="";
$subdom=substr($domain,7); // remove http://
preg_match("/[^\.\/]+\.[^\.\/]+$/", $domain, $domtld); // Get domain.tld.jpg
if(substr(strtolower($subdom),0,4)=="www.") {$dom=substr($subdom,4);}
else {$dom=$domtld[0];}
if(file_exists($homedir.$subdom.".jpg")) // If the full subdomain thumbnail exists, show it
{$thumbnail = "<a href='#'><img src='".$homedir.$subdom.".jpg' border='0' align='left' /></a>";}
elseif(file_exists($homedir.$dom.".jpg")) // Otherwise, show the domain.tld thumbnail, if it exists
{$thexists=1; $thumbnail = "<a href='#'><img src='".$homedir.$dom.".jpg' border='0' align='left' /></a>";}
elseif($stw==1) {$thumbnail = shrinktheweb('sm', $subdom);} // Otherwise, grab it from ShrinkTheWeb
else {$thumbnail = "<a href='#'><img src='".$homedir."nothumb.jpg' border='0' align='left' /></a>";} // If ShrinkTheWeb is down, show this default thumbnail
?>
If you prefer a PHP5 Class that accesses the ShrinkTheWeb API, please visit the ShrinkTheWeb Screenshot Plugins page.
Comments (0)
You don't have permission to comment on this page.