Help with Flickr Geolocation script!

Hi everybody, it's me again
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 
     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($ch0, CURLOPT_URL, $url0); 
    curl_setopt($ch0, CURLOPT_HEADER, 0); 
    curl_setopt($ch0, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch0, CURLOPT_FAILONERROR, TRUE); 
    $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($ch1, CURLOPT_URL, $url1); 
    curl_setopt($ch1, CURLOPT_HEADER, 0); 
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch1, CURLOPT_FAILONERROR, TRUE); 
    $image = curl_exec($ch1); 
    curl_close($ch1); 
    $return = substr( $image, 14, strlen($image)-15 ); 
    $json = json_decode( $return, true ); 

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

You are better off to work on your HTML formatting by trial and error.

I've made some progress :slight_smile:

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

Here is the 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($ch0, CURLOPT_URL, $url0); 
    curl_setopt($ch0, CURLOPT_HEADER, 0); 
    curl_setopt($ch0, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch0, CURLOPT_FAILONERROR, TRUE); 
    $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($ch1, CURLOPT_URL, $url1); 
    curl_setopt($ch1, CURLOPT_HEADER, 0); 
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch1, CURLOPT_FAILONERROR, TRUE); 
    $image = curl_exec($ch1); 
    curl_close($ch1); 
    $return = substr( $image, 14, strlen($image)-15 ); 
    $json = json_decode( $return, true ); 


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. :slight_smile:

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.
:slight_smile: