zoukankan      html  css  js  c++  java
  • flickr api 入门教程

    api文档地址:http://www.flickr.com/services/api/

    要求格式

    回應格式

    搞不懂要求格式和回应格式有什么区别。

    REST 要求格式

    REST 是要使用的最簡單的要求格式 - 它是簡單的 HTTP GET 或 POST 動作。

    REST 端點 URL 是 http://api.flickr.com/services/rest/

    若要要求 flickr.test.echo 服務,請按以下形式叫用:

    http://api.flickr.com/services/rest/?method=flickr.test.echo&name=value
    

    依預設,REST 要求將發送 REST 回覆

    相片來源 URL

    道相片的 ID、伺服器 ID、農場 ID 和密鑰(如許多 API 方法返回的那樣)後,你便可構建連結至相片的來源 URL

    URL 會採用以下格式:

    http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
    	or
    http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}_[mstzb].jpg
    	or
    http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{o-secret}_o.(jpg|gif|png)
    

    * 2011 年 11 月 18 日前,API 傳回的圖片 URL 帶有以下主機名稱: 「farm{farm-id}.static.flickr.com」。 目前仍支援這些 URL。

    後綴分類

    字母後綴如下:

    s 小正方形 75x75 这个常用,规格一样。
    q large square 150x150 这个规格一样
    t 縮圖,最長邊為 100
    m 小,最長邊為 240 规则不一样
    n small, 320 on longest side
    - 中等,最長邊為 500
    z 中等尺寸 640,最長邊為 640
    c 中等尺寸 800,最長邊為 800†
    b 大尺寸,最長邊為 1024*
    o 原始圖片, 根據來源格式可以是 jpg、gif 或 png

    *2010 年 5 月 25 日之前,大相片僅適用於特別大的原始圖片。

    † 僅於 2012 年 3 月 1 日後才可使用中等尺寸 800 相片。

    注意︰原始相片會有些許不同。他們有自己的密鑰(在回覆中稱為 originalsecret)和變數副檔名(在回覆中稱為 originalformat)。僅當呼叫程式具有檢視原始尺寸的權限(取決於使用者偏好和其他各種標準)時,透過 API 傳回這些值。這些值由 flickr.photos.getInfo 方法和諸如flickr.photos.search 等可傳回相片清單並允許 extras 參數(具有值 original_format)的所有方法來傳回。flickr.photos.getSizes 方法總是會傳回允許權限的完整的原始 URL。

    範例

    http://farm1.staticflickr.com/2/1418878_1e92283336_m.jpg
    
    farm-id: 1
    server-id: 2
    photo-id: 1418878
    secret: 1e92283336
    size: m
    

    網頁 URL

    至相片和個人檔案頁面的 URL 使用使用者的 NSID(其中帶有「@」符號的數字)或其自訂 URL(如果已經選擇了一個的話)。你可以呼叫flickr.people.getInfo 來尋找其自訂 URL。URL 的 NSID 版本將一直可用,不管他們是否已設定「完美」URL,因此你只需簡單地使用大多數相片 API 呼叫傳回的使用者 ID,就可以避免 API 呼叫。

    然後你就可以輕鬆建立至個人檔案、所有相片、個人相片或影集的 URL:

    http://www.flickr.com/people/{user-id}/ - profile
    http://www.flickr.com/photos/{user-id}/ - photostream
    http://www.flickr.com/photos/{user-id}/{photo-id} - individual photo
    http://www.flickr.com/photos/{user-id}/sets/ - all photosets
    http://www.flickr.com/photos/{user-id}/sets/{photoset-id} - single photoset
    

    同樣可以構建其他 URL。如果使用者已登入,則將他們導向至 http://www.flickr.com/photos/me/* 或 http://www.flickr.com/people/me/* 將使用其自己的使用者 ID 取代「me」,否則將重新導向至最新相片頁面。

    範例

    http://www.flickr.com/photos/12037949754@N01/
    http://www.flickr.com/photos/12037949754@N01/155761353/
    http://www.flickr.com/photos/12037949754@N01/sets/
    http://www.flickr.com/photos/12037949754@N01/sets/72157594162136485/
    

    簡短 URL

    Flickr 針對上載的相片(及視訊)提供縮短 URL 服務。簡短的 URL 在很多情形下都很有用,其中包括:電郵、名片、IM、文字訊息或簡短狀態更新。

    Flickr 上每張相片均擁有經數學計算的簡短 URL:

    http://flic.kr/p/{base58-photo-id}
    

    Base58 可用來使用字母和數字組合壓縮相片 ID。你可以在 Flickr API 群組中找到有關 base58 以及代碼範例的詳細資訊.

    基本例子:

    <?php 
    $url = 'http://api.flickr.com/services/rest/'; // 请求的URL地址
    $params = '?method=flickr.photos.search' . // method指明Flickr API所提供的某个方法
              '&api_key=2f59b5e190101271213d4b636e30824f' .  // Flickr分配的key
              '&text=sea'; // 关键字
    //生成的URL
    //http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2f59b5e190101271213d4b636e30824f&text=sea
    //如果把改URL黏贴到浏览器的地址栏里,同样可以得到XML文件
    $contents = file_get_contents($url . $params);
    $xml = new SimpleXMLElement($contents); // 解析XML文件
    foreach ($xml->photos->photo as $value) {
        $src = 'http://farm' . $value['farm'] . ".static.flickr.com/" .
        $value['server'] . '/' . $value['id'] . '_' . $value['secret'] . '_s.jpg'; // _s用来控制显示图片的大小
        echo "<img src=\"$src\" />";
    }
    ?>

    默认请求每页显示100张图片,可以使用per_page=number来指定具体的数量。更多参数可以查看Flickr官方的API,flickr.photos.search.html

    per_page (可選的)Number of photos to return per page. If this argument is omitted, it defaults to 100. The maximum allowed value is 500.

    page (可選的)The page of results to return. If this argument is omitted, it defaults to 1.

    tags (可選的)A comma-delimited list of tags. Photos with one or more of the tags listed will be returned. You can exclude results that match a term by prepending it with a - character.

    text (可選的)A free text search. Photos who's title, description or tags contain the text will be returned. You can exclude results that match a term by prepending it with a - character. 这里就是tags和text不同地方,text包括photo的标题,描述和标签。

    其中的原理清楚之后就可以用不同的编程语言来模拟请求,无非是找到某种适合发送GET或POST请求的函数。提供API接口的URL便会响应,传送数据到请求方,这样就可以对传回的数据进行处理应用

    上面的请求返回如下格式:

    <rsp stat="ok">

    <photos page="1" pages="103130" perpage="100" total="10312902">
     
    <photo id="8615411577" owner="12477292@N05" secret="7bb92c7a08" server="8525" farm="9" title="" ispublic="1" isfriend="0" isfamily="0"/>
     
    <photo id="8616496272" owner="52798229@N05" secret="0459a98587" server="8530" farm="9" title="a girl in a gold dress" ispublic="1" isfriend="0" isfamily="0"/>
     
    ...
    </photos>
    </rsp>
    解析xml用到了下面这个类
    final public SimpleXMLElement::__construct() ( string $data [, int $options = 0 [, bool $data_is_url = false [, string $ns = "" [, bool $is_prefix= false ]]]] )
    data

    A well-formed XML string or the path or URL to an XML document if data_is_url is TRUE.

    options

    Optionally used to specify additional Libxml parameters.

    data_is_url

    By default, data_is_url is FALSE. Use TRUE to specify that data is a path or URL to an XML document instead of string data.

    所以:

    $contents = file_get_contents($url . $params);
    $xml = new SimpleXMLElement($contents); // 解析XML文件
    可以写成:
    $xml=new SimpleXMLElement($url.$params,NULL,true);效果一样。


    建立自己相册的应用程序
    进入 http://www.flickr.com/services/api/explore/?method=flickr.people.getInfo
    右边有你的id说明。其中有一个为用户id,形式为:27769101@N03
    <?php
    $paramsArr=array(
    'method'=>'flickr.people.getPublicPhotos',
    'api_key'=>'xxx',
    'user_id'=>'xxx'
    );
    foreach($paramsArr as $k=>$v)
    {
        $params[]=urlencode($k).'='.urlencode($v);
    }
    //generate url
    $url='http://api.flickr.com/services/rest/?'.implode('&',$params);
    echo $url;
    $xml=new SimpleXMLElement($url,NULL,true);
    foreach($xml->photos->photo as $value)
    {
        $src='http://farm'.$value['farm'].'.static.flickr.com'.'/'.$value['server'].'/'.$value['id'].'_'.
         $value['secret'].'_q.jpg';
         echo "<img src=\"$src\" />";
    }
    ?>
         

    flickr.people.getPhotos 需要有读取权限的认证Return photos from the given user's photostream. Only photos visible to the calling user will be returned. This method must be authenticated; to return public photos for a user, useflickr.people.getPublicPhotos.

    flickr.people.getPublicPhotos 不需要认证



     
     
  • 相关阅读:
    BZOJ3578:GTY的人类基因组计划2(集合hash,STL)
    【BZOJ 1022】 [SHOI2008]小约翰的游戏John
    【BZOJ 1295】 [SCOI2009]最长距离
    【BZOJ 1103】 [POI2007]大都市meg
    【BZOJ 3172】 [Tjoi2013]单词
    【BZOJ 1067】 [SCOI2007]降雨量
    【BZOJ 1491】 [NOI2007]社交网络
    【BZOJ 1087】[SCOI2005]互不侵犯King
    【BZOJ 1009】 [HNOI2008]GT考试
    【BZOJ 1053】[HAOI2007]反素数ant
  • 原文地址:https://www.cnblogs.com/youxin/p/2998888.html
Copyright © 2011-2022 走看看