php – Flickr 的API入門 – 使用phpFlickr

該篇使用的API 是 phpFlickr
在這邊有http://www.flickr.com/services/api/
因為非官方設計的class,所以就PHP來說還有其他選擇。

下載後,會看到兩個非常重要的檔案
phpFlickr.php 主程式
auth.php 當flickr認證完以後要拋回來取得權限的PHP

申請完API會有兩個資料,一個是鑰匙(key),一個是密鑰(secret)

先打開auth.php
設定你的網站基本配置,這是我的範例

    $api_key                 = "XXX";
    $api_secret              = "XX";
    $default_redirect        = "/";
    $permissions             = "read";
    $path_to_phpFlickr_class = _BASEPATH . 'plugin/PHP/phpFlickr-3.1/';

喔對了,
請記得要去Flickr編輯你的API「認證流程」頁面
填寫『回呼 URL』,也就是要導向你的網站auth.php的頁面。

假設我得主程式網址
http://XXX.tw/hellow_flickr.php

取得權限的網址
http://xxx.tw/hellow_flickr_auth.php

hellow_flickr_auth.php
裡面可能會長這樣
include_once(“你的auth.php實際路徑”);
那麼回呼URL就要填寫:http://xxx.tw/hellow_flickr_auth.php

準備好以後我們就開始吧!
我們要做的是取得某相簿裡尺寸small的所有照片(當然也可以取得原始尺寸啦XD)

                //引用路徑
		include(_BASEPATH . 'plugin/PHP/phpFlickr-3.1/phpFlickr.php');


		$f = new phpFlickr("你的key", "你的密鑰");

		//授權登入, 若沒登入flick他會幫你導向yahoo登入頁面喔,登入完以後導向你API設定的『回呼URL』,再從回呼URL導到這個頁面並繼續往下跑~
		$f->auth();


		// 1)自己所有的相簿集
		$res			= $f->photosets_getList();
		$PhotosetList	= $res['photoset'];
		//print_r($PhotosetList);


		// 2)假設我相簿ID 72157633613559873, 取得該相片集裡頭的所有相片資料
		$Allphoto = $f->photosets_getPhotos('72157627211913851');
		//print_r($Allphoto);
		/*
		大概會長這樣
		Array
		(
			[photoset] => Array
				(
					[id] => xxxxxxxxxxxxxx
					[primary] => xxxxxxxxxxxxxxxx
					[owner] => xxxxxxxxxxxxxxxxxxxxx
					[ownername] => xxxxxxxxxxxxxxxxx
					[photo] => Array
						(
							[0] => Array
								(
									[id] => xxxxxxxxxxx
									[secret] => xxxxxxxxxxxxxx
									[server] => xxxxxxxxxxxxx
									[farm] => xxxxxxxxxxxxx
									[title] => xxxxxxxxxxxx
									[isprimary] => xxxxxxxxxxxxxxxxx
								)
						)
				)
		)

		*/



		// 3)輸出所有照片吧
		$PhotoList = $Allphoto['photoset']['photo'];
		if (is_array($PhotoList)) foreach ($PhotoList as $PhotoInfo)
		{
			/*
				$PhotoInfo 的內容是這樣
				[鍵] => Array
					(
						[id] => xxxxxxxxxxx
						[secret] => xxxxxxxxxxxxxx
						[server] => xxxxxxxxxxxxx
						[farm] => xxxxxxxxxxxxx
						[title] => xxxxxxxxxxxx
						[isprimary] => xxxxxxxxxxxxxxxxx
					)
			*/

			/*
			假設我要取得small的尺寸
			*/


					/*
					方法A ==========================================
					如果使用 $f->photos_getSizes 因為還要再傳API到Flickr,當數量多比以後,跑起來就會龜到不行,強烈不建議。
					該API似乎沒有像SQL 這種where in ('123,456')一次查詢多筆的方法(FB好像可以)。
					*/
					//取得各種尺寸
					/*
					$Array = $f->photos_getSizes($PhotoInfo['id']);
					?><img src="<?=$Array[3]['source'];?>"><?
					//print_r($Array);
					*/

					/*
					方法B ==========================================
					這個phpFlickr class 提供自動拼湊文字成網址,這樣就不用去server調資料,當然速度飛快多了。
					所以我們使用它提供的buildPhotoURL()。
					有興趣可以看一下網址的拼湊規則
					其實就是
					$imgsrc = "http://farm{$PhotoInfo[farm]}.static.flickr.com/{$PhotoInfo[server]}/{$PhotoInfo[id]}_{$PhotoInfo[secret]}_m.jpg";
					*/
					$imgsrc = $f->buildPhotoURL($PhotoInfo, "small");
					?><img src="<?=$imgsrc?>"><?



		}

這樣就完成了~~

目前Flickr提供1TB的容量,最大的重點就是拿來當圖床囉!
差不多最重要得事情就是這個了…………….呵呵