#!/usr/local/bin/php
<?php
/* this is a simple command line script example of how to request a valid thumbnail url */
/* SHELL# ./thumbnail-example.php mysite.com */

function main ($domainName NULL$type 'info/json') {
    if (empty (
$domainName) || empty ($type)) return false;
    
$secret "mytopsecretcodenooneelsewillhave"// <-- your secret code, provided by your account representative
    
$partnerName "partnername"// <-- your partner name, provided by your account representative
    
$timestamp NIResourceAuthenticator::getTimestamp ();
    
$hash NIResourceAuthenticator::getHash ($domain$timestamp,$partnerName$secret);
    
$url "http:/thumbnails.nameintel.com/" $partnerName "/" $timestamp "/" $hash "/" $domainName "/" $type;
    return 
$url;
}

if (
array_key_exists (1$argv)) {
   
$url $argv[1];
} else { 
$url 'cnn.com'; }
echo 
main ($url) . "\n"// echoes the resulting URL to your terminal.
?>
<?php 
 
/*
  * Name Intelligence Resource Authenticator
  * Generates an authentication hash and string to request
  * a resource from Name Intelligence
  */
class NIResourceAuthenticator {
    
/**
     * Get the authetnication hash for a given partner, timestamp, domain, and secret key
     *
     * @param string $permitted_resource_id Id of resource to authenticate with this hash
     * @param string $timestamp Time of authentication, can be null or a unix ts
     * @param string $partnerID Partner account name
     * @param string $secret Secret key assigned to the partner
     * @return string Hash with proper URL encoding
     */
    
public static function getHash(
        
$permitted_resource_id
        
,$timestamp null
        
,$partner_name
        
,$secret
    
) {
        if(
is_null($timestamp)||is_int($timestamp)){
            
$timestamp self::getTimestamp($timestamp);
        }
        
$data $partner_name $timestamp $permitted_resource_id $secret;
        
$encoded base64_encode (
           
pack("H*"sha1((str_pad($secret64chr(0x00))
           ^(
str_repeat(chr(0x5c), 64))) .
           
pack("H*"sha1((str_pad($secret64chr(0x00))
           ^(
str_repeat(chr(0x36), 64))) . $data))))
        ); 
        
$encoded str_replace(array("+","/"), array("-","_"),$encoded) ;
        return 
$encoded;
    }

    
/**
     * Generates a timestamp in the correct format to pass to the resource proxy
     * @param int $time the time to specify for the hash
     * @return string Correctly formatted timestamp for the current or specified time
     */
    
public static function getTimestamp($time=null) {
        if(
is_null($time)){
            
$time time();
        }
        return 
gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z",$time); 
    }

}