Visit The New, Modern Unix Linux Community


Help with Flickr Geolocation script!


 
Thread Tools Search this Thread
Top Forums Web Development Help with Flickr Geolocation script!
# 1  
Help with Flickr Geolocation script!

Hi everybody, it's me again Image
I'm currently working on a script that will show a website visitor pictures of their location. The pictures come from the Flickr API and the address is grabbed from HostIP.
PHP Code:
<?php 
     define
('SORT''&sort=relevance&page=1'); 
    
define('QUERY_0''http://api.hostip.info/get_html.php?ip='); 
    
define('QUERY_1''http://api.flickr.com/services/rest/?format=json&method=flickr.photos.search&api_key=*******&tags='); 

function 
getImage() { 
     
   if (!empty(
$_SERVER['HTTP_CLIENT_IP'])) 
    { 
        
$ip $_SERVER['HTTP_CLIENT_IP']; 
    } elseif (!empty(
$_SERVER['HTTP_X_FORWARDED_FOR'])) 
    { 
        
$ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
    } 
    else 
    { 
        
$ip $_SERVER['REMOTE_ADDR']; 
    } 

    
$url0 QUERY_0.$ip
    
$ch0 curl_init(); 
    
curl_setopt($ch0CURLOPT_URL$url0); 
    
curl_setopt($ch0CURLOPT_HEADER0); 
    
curl_setopt($ch0CURLOPT_RETURNTRANSFERTRUE); 
    
curl_setopt($ch0CURLOPT_FAILONERRORTRUE); 
    
$geo curl_exec($ch0); 
    
curl_close($ch0); 
    if( 
preg_match('/City: (.*)\nIP:/'$geo$matches) ){ 
        
$results $matches[1]; 
    }else{ 
        
$results 'getLocation failure'

    } 

    
$location urlencode($results); 
    
$url1 QUERY_1.$location.SORT
    
$ch1 curl_init(); 
    
curl_setopt($ch1CURLOPT_URL$url1); 
    
curl_setopt($ch1CURLOPT_HEADER0); 
    
curl_setopt($ch1CURLOPT_RETURNTRANSFERTRUE); 
    
curl_setopt($ch1CURLOPT_FAILONERRORTRUE); 
    
$image curl_exec($ch1); 
    
curl_close($ch1); 
    
$return substr$image14strlen($image)-15 ); 
    
$json json_decode$returntrue ); 

foreach( 
$json['photos']['photo'] as $i => $d ) { 
    
$url2 'http://farm' $d['farm'] . '.static.flickr.com/' $d['server'] . '/' $d['id'] . '_' $d['secret'] . '_b.jpg'
     
    echo 
'<img src="' $url2 '" />'


getImage(); 

?>
I used an example from here to call on the Flickr API:

get large flickr photos based on topic - PHP - Snipplr

I don't know very much about JSON or PHP for that matter, LOL. What I would like to do is limit the results to 3 and have them in a nice row with each image being a thumbnail size. Anybody got any ideas or examples of how to do this? I'd really appreciate some help, thanks Image
# 2  
You are better off to work on your HTML formatting by trial and error.
# 3  
Quote:
Originally Posted by Neo
You are better off to work on your HTML formatting by trial and error.
I've made some progress Smilie

http://img696.imageshack.us/img696/1...reenshotle.png

Here is the code:
PHP Code:
<?php 

    define
('SORT''&sort=relevance&per_page=5'); 
    
define('QUERY_0''http://api.hostip.info/get_html.php?ip='); 
    
define('QUERY_1''http://api.flickr.com/services/rest/?format=json&method=flickr.photos.search&api_key=9f2426e946367be0e9d871c89f6d2005&tags='); 

function 
getImage() { 
     
   if (!empty(
$_SERVER['HTTP_CLIENT_IP'])) 
    { 
        
$ip $_SERVER['HTTP_CLIENT_IP']; 
    } elseif (!empty(
$_SERVER['HTTP_X_FORWARDED_FOR'])) 
    { 
        
$ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
    } 
    else 
    { 
        
$ip $_SERVER['REMOTE_ADDR']; 
    } 

    
$url0 QUERY_0.$ip
    
$ch0 curl_init(); 
    
curl_setopt($ch0CURLOPT_URL$url0); 
    
curl_setopt($ch0CURLOPT_HEADER0); 
    
curl_setopt($ch0CURLOPT_RETURNTRANSFERTRUE); 
    
curl_setopt($ch0CURLOPT_FAILONERRORTRUE); 
    
$geo curl_exec($ch0); 
    
curl_close($ch0); 
    if( 
preg_match('/City: (.*)\nIP:/'$geo$matches) ){ 
        
$results $matches[1]; 
    }else{ 
        
$results 'getLocation failure'

    } 

    
$location urlencode($results); 
    
$url1 QUERY_1.$location.SORT
    
$ch1 curl_init(); 
    
curl_setopt($ch1CURLOPT_URL$url1); 
    
curl_setopt($ch1CURLOPT_HEADER0); 
    
curl_setopt($ch1CURLOPT_RETURNTRANSFERTRUE); 
    
curl_setopt($ch1CURLOPT_FAILONERRORTRUE); 
    
$image curl_exec($ch1); 
    
curl_close($ch1); 
    
$return substr$image14strlen($image)-15 ); 
    
$json json_decode$returntrue ); 


foreach(
$json['photos']['photo'] as $i => $d )  { 
$url2 'http://farm' $d['farm'] . '.static.flickr.com/' $d['server'] . '/' $d['id'] . '_' $d['secret'] . '_b.jpg'

echo 
'<img src="' $url2 '" width="200" height="200" />'



getImage(); 

?>
Fix: I added the "&per_page=5" parameter to the "SORT" definition, and "width="200" height="200"" to the final "echo" statement. Smilie

The only problem I am having now is that some of the images being returned are unavailable (See Picture Above). How would I remove them from the output altogether? Is the some way to check for inactive links and remove them? Can I use the "photo_unavailable.gif" file-size (2.7KB) to filter inactive images out of the results, or how about just filtering out ".gifs" completely?; because the good images are all ".jpg"

BTW, these images are not stored on my server.

Thanks again.
Smilie

Last edited by o0110o; 02-24-2010 at 01:25 PM..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #939
Difficulty: Easy
The author named the ping utility after the sound that a sonar makes.
True or False?

4 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

2. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

3. Shell Programming and Scripting

Grab all jpg's from flickr

Hi everyone, i'm trying to download all the jpg's that my school put on flickr and im trying to do this with wget via a bash script. The pictures that im trying to pull are on this site : and below is my script that doesn't appear to be working (1 Reply)
Discussion started by: binary-ninja
1 Replies

4. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies

Featured Tech Videos