", " " , $dirtyterms);
$terms = ereg_replace("[^A-Za-z ]","", $dirtyterms);
$numresults = ereg_replace("[^0-9]","", $dirtynum);
#echo $numresults . $terms;
$ref = $_SERVER['HTTP_REFERER'];
$ser = "Server: ". $_SERVER['SERVER_NAME'] . "Script name: ". $_SERVER['SCRIPT_NAME'];
#echo "document.write('Hello World I am php code called from HTML file:' );";
#echo "document.write('$ref');";
#echo "document.write('$ser');";
function download_page($path){
#echo ("path of call " . $path . "
");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
##### cleanup the input ######
if ($terms == "" ) $terms="travel";
$terms= str_ireplace ("Free Stock Image Gallery Main Page", "travel" , $terms);
$terms= str_ireplace (" and ", " " , $terms);
$terms= str_ireplace (" a ", " " , $terms);
$terms= str_ireplace (" of ", " " , $terms);
$terms= str_ireplace ("photos ", " " , $terms);
$terms= str_ireplace ("collection ", " " , $terms);
$terms= str_ireplace ("selection ", " " , $terms);
$terms= str_ireplace ("photo ", " " , $terms);
$terms= str_ireplace ("images ", " " , $terms);
$terms= str_ireplace ("free stock", " " , $terms);
$terms= str_ireplace ("free ", " " , $terms);
$terms= str_ireplace ("gallery ", " " , $terms);
$terms= str_ireplace ("index ", " " , $terms);
$terms= str_ireplace (" index", " " , $terms);
$terms= trim ($terms);
if ($numresults == "" ) $numresults=11;
if ($numresults == " " ) $numresults=11;
$urlterms = str_replace (" ", "+" , $terms);
$fileterms = str_replace (" ", "_" , $terms);
$cachefname = "cache/".($fileterms).".txt";
#echo ("document.write('terms are " . $urlterms . "');");
#echo ("cachefile is " . $cachefname . "\n");
#echo "
";
###### open the log file
$loghandle = fopen("apiaccesslog.txt", "a");
###### check to see if we already have some results
if (file_exists($cachefname)) {
#echo ("results from cache");
#echo ("cachefile in scope is " . $cachefname . "\n");
#echo ("printing cached from file " . $cachefname . " number of results requested " . $numresults . "
");
$resultshandle = fopen($cachefname, "r");
if ( filesize($cachefname) > 0 ) {
$jresults= fread($resultshandle, filesize($cachefname));
}
else
{
echo "No results file for " . $terms;
}
fclose($resultshandle);
$results = json_decode($jresults);
if ( $results->count < "$numresults" ){
$numresults = $results->count;
}
for ($i=0; $i<$numresults; $i++) {
#echo ($i);
echo ("
");
$output = ("result[$i]->itemurl) . "&source=pe_context\" target=\"_blank\" rel=\"nofollow\">
result[$i]->medium_thumbnail . "\"/> ");
#echo "here";
#echo ("document.write('well hello there');");
echo ( $output );
echo ("");
}
#echo ("cachefile in scope is " . $cachefname . "\n");
}
else {
#echo ("getting live results");
#echo "
";
sleep (5);
#### get some results
$resultscount=0;
while ($resultscount == 0)
{
$jresults = download_page('http://api.depositphotos.com?dp_command=search&dp_apikey=7818980a374e638f8dfd18a9b0fb23da294afffb&dp_search_query=' . $urlterms . '&dp_search_limit=20');
#echo $jresults;
#echo "
";
$results = json_decode($jresults);
#print_r ($results);
$resultshandle = fopen($cachefname, "w");
#echo "There were " . $results->count . " results for your search " . $terms;
$resultscount=$results->count;
if ($resultscount < 6) {
$resultscount = 0; # so that we go round again
sleep (5);
$phrase_array = explode(' ',$terms);
#echo ("number of terms was " . count($phrase_array) . "
" );
if(count($phrase_array) > 1 ) {
$terms = implode(' ',array_slice($phrase_array, 0, count($phrase_array)-1 ));
$flag=(" (modified from ". $urlterms . ")");
#$explosion= explode(" ", $terms);
#$terms=$explosion[0];
#echo ("terms modified to" . $terms);
$urlterms = str_replace (" ", "+" , $terms);
}
else
{
#there was only 1 term and still ness than 6 results
$resultscount=-1;
}
} # end check 0 results count
# NOTE the log file reads a little quirky sain that there wasre 0 results for "keyword" modified from "keyword othere" then prints a second line to replace it
$log = (date(DATE_RFC822) . " dpcontext Found ". $results->count ." results for \"". $terms . $flag . "\" at depositphotos\n");
if ($results->type != 'success') {
$log = (date(DATE_RFC822) . " api return to not success ". $results->count ." results for \"". $terms . $flag . "\" at depositphotos\n");
# overwrite the log strng if there was a fault
}
if (fwrite($loghandle, $log) === FALSE) {
echo "Cannot write to file dpcontext_984_04";
return(-1);
}
if ($results->type != 'success') {
return (-1);
# drop out right away dont cache the results if there was a fault
}
if (fwrite($resultshandle, $jresults) === FALSE) {
echo "Cannot write to file dpcontext_4894_91";
exit;
}
fclose ($resultshandle);
} #end while results count is 0
#echo ("printing cached first loop " . $cachefname . " number of results requested " . $numresults . "
");
$resultshandle = fopen($cachefname, "r");
if ( filesize($cachefname) > 0 ) {
$jresults= fread($resultshandle, filesize($cachefname));
}
else
{
echo "No results file for " . $terms;
}
fclose($resultshandle);
$results = json_decode($jresults);
if ( $results->count < "$numresults" ){
$numresults = $results->count;
}
for ($i=0; $i<$numresults; $i++) {
#echo ($i);
echo ("
");
$output = ("result[$i]->itemurl) . "&source=pe_context\" target=\"_blank\" rel=\"nofollow\">
result[$i]->url . "\"/> ");
#echo "here";
#echo ("document.write('well hello there');");
echo ( $output );
echo ("");
}
#echo ("cachefile in scope is " . $cachefname . "\n");
}
fclose($loghandle);
?>