phpapiflickr

Using the Flickr API to Pull Images in PHP


I am trying to pull images using the Flickr API, for photos which have the tag 'Southampton'. However, with my below code, none of the photos are showing...

<?php
        $tag = 'southampton';          
        $url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=APIKEYHERE&tags='.$tag.'&format=json&nojsoncallback=1';
        $data = json_decode(file_get_contents($url));
        $photos = $data->photos->photo;
        foreach ($photos as $photo){
                        
       $url = 'http://farm'.$photo->farm.'staticflickr.com/'.$photo->server.'/'.$photo->id.'_'.$photo->secret.'.jpg';
            
       echo '<img src="'.$url.'">'; }}
?>

What could be causing the flickr images to not show?

When I echo the URL I am getting back invalid Flickr URL's like the below:

http://farm66staticflickr.com/65535/50898322013_69261bacb6.jpg Is there anything I have missed from the above code which could be causing this?


Solution

  • You missing a "dot" between the farm and the domain (".staticflickr.com") :

    $url = 'http://farm'.$photo->farm.'.staticflickr.com/'.$photo->server.'/'.$photo->id.'_'.$photo->secret.'.jpg';
    //                                 ^ this dot
    

    Will generate

    http://farm66.staticflickr.com/65535/50898322013_69261bacb6.jpg