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 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($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