Home > PHP > いまさらFlickrのAPIを使ってみたよ!

いまさらFlickrのAPIを使ってみたよ!

  • 2007-05-25 (金) 22:53
  • PHP

今まで、他人が作ったマッシュアップは大量に見てきたけど自分でAPIを使った何かを作ったことが無かったので、今更FlickrのAPIを利用したギャラリーを作ってみたよ!

PHPを選んだ理由は特別無く、Perlはデバックが面倒な印象があったので、 「それならPHP勉強してみるか〜」っというだけ。
http://flickr.shinla.org/

  • PHP5専用
  • なんちゃってオブジェクト指向
  • サムネイルクリック時にLightBoxとFlickrへ飛ばすか選べるよ!

以下、コードと反省点などなど。



	class common_Data
	{
		//プロパティ
		public $myapi_key = 'ここは自分のAPIキーを入れてくだしあ';//Flickr API KEY
		public $myuser_name = 'suniti';//自分のユーザーネーム
		public $per_page = '50';//1ページに表示する枚数(最大500件)
		public $url_flg = '1';//写真のリンク先設定(1=LightBoxで表示 2=Flickrで表示)
		public $filename = 'index.php';//ファイル名

		public $myuser_id;//ユーザーID
		public $get_page;//表示しているページ
		public $get_pages;//全体のページ数
		public $total_photos;//写真の総数

		// ライセンス
		public $app_name = 'My Flickr Photo';
		public $version = '0.1';
		public $author = 'grayash.com';
		public $url = 'http://blog.grayash.com/';

		//コンストラクタ
		function __construct() {
			//ユーザーIDの処理
			if($this->myuser_id == ''){
				$this->myuser_id = ViewMyFlickrPhoto::convert_userid($this->myuser_name);
				$this->user_photo_url =	ViewMyFlickrPhoto::get_userurl($this->myuser_id);
			}
		}
	}

	class ViewMyFlickrPhoto extends common_Data {
		//ユーザーネームから、ユーザーIDを取得
		function convert_userid($u_name) {
			$rest = 'http://www.flickr.com/services/rest/?method=flickr.people.findByUsername';
			$rest .='&api_key=' .$this->myapi_key;
			$rest .='&username=' .$u_name;
			$article = simplexml_load_file($rest);

			return $article->user[id];
		}

		//ユーザーIDから写真が格納してあるURLを取得 
		//(ユーザーネームと違う場合があるから(変更した人?))
		function get_userurl($u_id) {
			$rest = 'http://www.flickr.com/services/rest/?method=flickr.people.getInfo';
			$rest .='&api_key=' .$this->myapi_key;
			$rest .='&user_id=' .$u_id;
			$article = simplexml_load_file($rest);

			return $article->person->photosurl;
		}

		//写真を取得
		function get_userphoto($u_page) {
			//URL生成
			$rest  ='http://www.flickr.com/services/rest/?method=flickr.photos.search';
			$rest .='&api_key=' .$this->myapi_key;
			$rest .='&user_id=' .$this->myuser_id;
			$rest .='&per_page=' .$this->per_page;

			//ページ処理
			if($u_page != ''){
				$rest .='&page=' .$u_page;
			}

			//接続&XML解析
			$article = simplexml_load_file($rest);

			$this->get_page			= $article->photos["page"];
			$this->get_pages		= $article->photos["pages"];
			$this->total_photos	= $article->photos["total"];

			foreach ($article->photos->photo as $photo) {
				$str  ='http://farm';
				$str .=$photo["farm"];
				$str .='.static.flickr.com/';
				$str .=$photo["server"];
				$str .='/';
				$str .=$photo["id"];
				$str .='_';
				$str .=$photo["secret"];

				if($this->url_flg == 2){
					//Flickr利用の処理
				}
				//LightBoxの場合
				else if($this->url_flg == 1){
					//LightBox利用の処理
				}
			}
		}

		//エラー表示
		function error($value) {
			echo '次のエラーが出てるで?:' .$value;
		}
	}
##################################################################################################
##  メイン処理
##################################################################################################
	$tml =	new ViewMyFlickrPhoto();
	$tml->get_userphoto($_GET["page"]);
	$tml->page_control();
	$tml->license();

反省点

  • ページ出力の処理がダセェ。(WordPressの問題でココには書いてません)
  • なんちゃってオブジェクト指向 → いまいち理解しきれなかった。
    • デザイナーから見たら手続き型の方が理解しやすい。
  • オブジェクト指向を使う意味が掴めなかった。やっぱり小規模じゃダメかな?
  • 「重い」 FlickrのAPIが鈍いのか、プログラムの組み方が悪いのか・・・?原因不明><

勉強になった点

  • PHP4とPHP5で全然違うんだね! PHP5は便利だ!ありがとうsimplexml!
  • APIって面白い!Web2.0って素敵!
  • へー、クラスってこうやって使うのか・・・

Comments:0

Comment Form
Remember personal info
Macをはじめよう。Apple Store(Japan)

Trackback+Pingback:0

TrackBack URL for this entry
http://blog.grayash.com/archives/504/trackback
Listed below are links to weblogs that reference
いまさらFlickrのAPIを使ってみたよ! from blog.grayash.com

Home > PHP > いまさらFlickrのAPIを使ってみたよ!

Search
Google
Feeds
Meta
あわせて読む
あわせて読みたい
Yahoo! ログール読む

Return to page top