<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Flickr_model {

	var $_apikey = '0023b780d24ed697f54b9e8682cb5386';

    function __construct()
    {		
    }

	public function search($search, $count = 30)
	{
		$url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key='.$this->_apikey.'&per_page='.$count.'&text='.urlencode($search);
		$ch = curl_init( $url );
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
		$data = curl_exec( $ch );
		curl_close( $ch );
		$xml = simplexml_load_string($data);
		if($xml === false || !$xml->photos->photo || count($xml->photos->photo) == 0) {
			return false;
		}
		
		foreach ($xml->photos->photo as $i) {
			$image = 'http://farm'.(string)$i->attributes()->farm.'.static.flickr.com/'.(string)$i->attributes()->server.'/'.(string)$i->attributes()->id.'_'.(string)$i->attributes()->secret.'_m.jpg';
			$title = (string)$i->attributes()->title;
			$url = 'http://www.flickr.com/photos/'.(string)$i->attributes()->owner.'/'.(string)$i->attributes()->id.'/';
			
			if($image != '' && $title != '' && $url != '') {
				$result[] = array(
					'title' => $title,
					'image' => $image,
					'url' => $url
				);
			}
		}
			
		$result = array_map('unserialize', array_unique(array_map('serialize', $result)));

		return $result;
	}
}
