Archivio

Posts Tagged ‘Php’

Flickr estrazione di una singola foto con tag

27 Settembre 2017 Nessun commento
1 Star2 Stars3 Stars4 Stars5 Stars (Ancora nessun voto)
Loading...

Ecco una piccola funzione che vi potrà essere utile per estrarre una singola foto da quelle disponibili attraverso un singolo tag. Si ricorda che per poter usare la funzione bisogna aver richiesto la key di Flickr:
function Photo(){
//Setting the url
//For more setting read: https://www.flickr.com/services/api/flickr.photos.search.html
$api_key = 'xxxxxxxx';
$tag = 'udine';
$perPage = 500; //Number of photos to return per page. If this argument is omitted, it defaults to 100. The maximum allowed value is 500.
$url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search';
$url.= '&api_key='.$api_key;
$url.= '&tags='.$tag;
$url.= '&per_page='.$perPage;
$url.= '&format=json';
$url.= '&nojsoncallback=1';

//Extract
$json = file_get_contents($url);
$response = json_decode($json, TRUE);
$photo_array=(array)$response['photos']['photo'];

//Function array_rand for any single photo
$n = array_rand($photo_array, 1);

$single_photo = $photo_array[$n];
$farm_id = $single_photo['farm'];
$server_id = $single_photo['server'];
$photo_id = $single_photo['id'];
$secret_id = $single_photo['secret'];
$title = $single_photo['title'];

$size = 'z'; //format/dimension photo
$photo_url = 'http://farm'.$farm_id.'.staticflickr.com/'.$server_id.'/'.$photo_id.'_'.$secret_id.'_'.$size.'.'.'jpg';
$title = "Foto: ".$title."\n ".$photo_url;

return $title;
}

Buon divertimento!

Categorie:Php, Programmazione Tag: , , , ,