phpflickrphpflickr

Flickr API returning duplicate images


I'm trying to get images from flickr using the Flickr API and I'm having trouble figuring out how to get only unique images. I've already reviewed the arguments for the specific method that I'm using and here's what I came up with:

<?php
class Flickr {

    private $flickr_key;
    private $flickr_secret;
    private $format = 'json';

    public function __construct( $flickr_key, $flickr_secret ) {

        $this->flickr_key = $flickr_key;
        $this->flickr_secret = $flickr_secret;
    }


    public function searchPhotos( $query = '', $tags = '' ) {

        $urlencoded_tags = array(); 

        $tags_r = explode(',', $tags);
        foreach($tags_r as $tag){
            $urlencoded_tags[] = urlencode($tag);
        }

        $url = 'http://api.flickr.com/services/rest/?';
        $url .= 'method=flickr.photos.search';
        $url .= '&text=' . urlencode($query);
        $url .= '&tags=' . implode(',', $urlencoded_tags);
        $url .= '&sort=relevance';
        $url .= '&safe_search=1';
        $url .= '&content_type=4';
        $url .= '&api_key=' . $this->flickr_key;
        $url .= '&format=' . $this->format;
        $url .= '&per_page=10';
        $url .= '&media=photos';
        $url .= '&privacy_filter=1';

        $result = @file_get_contents( $url );


        $json = substr( $result, strlen( "jsonFlickrApi(" ), strlen( $result ) - strlen( "jsonFlickrApi(" ) - 1 );

        $photos = array();
        $data = json_decode( $json, true );
        if($data['stat'] != 'fail'){
            $photos = $data['photos']['photo'];
            return $photos;
        }else{
            return false;
        }
    }
}

And I'll just call it in like:

$flickr = new Flickr($flickr_key, $flickr_secret);

$query = 'Kaspersky Internet Security 2013';
$tags = 'software';

$results = $flickr->searchPhotos($query, $tags);
foreach($results as $img){
    $src = "http://farm" . $img['farm'] . ".static.flickr.com/" . $img['server'] . '/' . $img['id'] . '_' . $img['secret'] . '_m.jpg';
?>
  <img src="<?php echo $src; ?>"/>
<?php
}

The problem here is that I'm getting duplicate images from time to time. I also tried using the phpflickr library. But I'm still having the same issues:

$flickr = new phpFlickr($api_key);

$args = array(
    'text' => 'Kaspersky Internet Security 2013',
    'tags' => 'software',
    'per_page' => '10',
    'safe_search' => '1',
    'content_type' => '4',
    'media' => 'photos',
    'sort' => 'relevance',
    'privacy_filter' => '1'
);

$results = $flickr->photos_search($args);

$hashes = array();
$sources = array();

$images = $results['photo'];

foreach($images as $img){

    $src = "http://farm" . $img['farm'] . ".static.flickr.com/" . $img['server'] . '/' . $img['id'] . '_' . $img['secret'] . '_m.jpg';

    $current_hash = sha1_file($src);


    if(!in_array($current_hash, $hashes)){
?>
    <img src="<?php echo $src; ?>" alt="">
<?php       
    }

    $hashes[] = $current_hash;
}

As you can see from the above code I've used sha1_file method to compare the hashes of each of the images returned from flickr. But that's a big performance hit:

without sha1_file: 0.81311082839966
with sha1_file: 6.8974900245667 

Any ideas what else can I do to prevent flickr from returning duplicates? As you can see I'm only returning 10 images and that's all I need. I've also tried to add as many arguments that matches my needs but still no luck. Thanks in advance!


Solution

  • try on API explorer, will this give you the same result?

    I tried using the param you specified, it returns 34 result in total..