The App Garden on Flickr - http://www.flickr.com/services/

想要使用 Flickr API 來玩一些有趣的東西,那必須先到官網申請一組 KEY 來使用。

  1. 連到 The App Garden on Flickr - http://www.flickr.com/services/
  2. 點選 [Create an App] -> [Get your API Key] -> [Request an API Key] ,在此挑選 [Non-Commercial] ,接著輸入你想要的應用程式的名字跟簡介。
  3. 最後,就會得到一組序號,分別是 Key 和 Secret

接著寫簡單的 PHP 程式作為 Demo 方式:

  • 預計使用 flickr.photos.search 功能,使用參數如下:
    • api_key
    • format
    • tags
    • per_page
    • page

程式碼(在此使用 REST Request Format 與 JSON 格式):

<?php

$target_url = 'http://api.flickr.com/services/rest/?';
$data = array(
        'method' => 'flickr.photos.search' ,
        'format' => 'json' ,
        'nojsoncallback' => 1 ,
        'api_key' => 'YOUR_API_KEY' ,
        'per_page' => 3 ,
        'tags' => 'hello world'
);      

$target_url .= http_build_query( $data );

$ch = curl_init();
curl_setopt( $ch , CURLOPT_URL , $target_url );
curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true );
$d = curl_exec( $ch );
curl_close( $ch );

$d = json_decode( $d );
print_r( $d );
exit;
?>

輸出:

stdClass Object
(
    [photos] => stdClass Object
        (
            [page] => 1
            [pages] => 391
            [perpage] => 3
            [total] => 1171
            [photo] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 4539740346
                            [owner] => 21229296@N03
                            [secret] => 3b10921450
                            [server] => 4040
                            [farm] => 5
                            [title] => ~ Berry One ~
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [1] => stdClass Object
                        (
                            [id] => 4539723084
                            [owner] => 21229296@N03
                            [secret] => d5fe05dba2
                            [server] => 2698
                            [farm] => 3
                            [title] => ||  Narcissistic Paradox - Single Flower  ||
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [2] => stdClass Object
                        (
                            [id] => 4538993631
                            [owner] => 21229296@N03
                            [secret] => afe8fd4a2a
                            [server] => 2793
                            [farm] => 3
                            [title] => Snowzuki X90 #3
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                )

        )

    [stat] => ok
)

另外,也可以直接用以下方便的 framework

以 phpFlickr 為例:

程式碼:

<?php

require_once( "phpFlickr/phpFlickr.php" );

$o = new phpFlickr( 'YOUR_API_KEY' );
$d = $o->photos_search( array(
                                'tags' => 'hello world' ,
                                'per_page' => 3
                        )   
        );  
print_r( $d );
?>

輸出:

Array
(
    [page] => 1
    [pages] => 391
    [perpage] => 3
    [total] => 1171
    [photo] => Array
        (
            [0] => Array
                (
                    [id] => 4539740346
                    [owner] => 21229296@N03
                    [secret] => 3b10921450
                    [server] => 4040
                    [farm] => 5
                    [title] => ~ Berry One ~
                    [ispublic] => 1
                    [isfriend] => 0
                    [isfamily] => 0
                )

            [1] => Array
                (
                    [id] => 4539723084
                    [owner] => 21229296@N03
                    [secret] => d5fe05dba2
                    [server] => 2698
                    [farm] => 3
                    [title] => ||  Narcissistic Paradox - Single Flower  ||
                    [ispublic] => 1
                    [isfriend] => 0
                    [isfamily] => 0
                )

            [2] => Array
                (
                    [id] => 4538993631
                    [owner] => 21229296@N03
                    [secret] => afe8fd4a2a
                    [server] => 2793
                    [farm] => 3
                    [title] => Snowzuki X90 #3
                    [ispublic] => 1
                    [isfriend] => 0
                    [isfamily] => 0
                )

        )

)

最後,則是要組出圖片的 static url ,請參考相片來源 http://www.flickr.com/services/api/misc.urls.html

  • http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}.jpg
  • http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_[mstb].jpg
  • http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{o-secret}_o.(jpg|gif|png)

創作者介紹

第二十四個夏天後

changyy 發表在 痞客邦 PIXNET 留言(0) 人氣()