问题描述
我正在用 Rails 构建我的第一个应用程序,我想调用 Flickr 的 API
Im building my first app in rails and I would like to call Flickr's API
我知道我可以使用 flickr-fu、rflickr 或其他 gem,但我想自己从头开始学习
I know I can use flickr-fu, rflickr or other gem but I want to do it myself from scratch to learn
例如,我想调用返回照片评论的 flickr.photos.comments.getList
.参数是 api_key 和 photo_id.我两个都有.
Say, for example, I want to call flickr.photos.comments.getList
that returns the comments for a photo. The arguments are api_key and photo_id. I have both.
我将得到一个 xml 格式的响应
I will get the response as an xml
<comments photo_id="109722179">
<comment id="6065-109722179-72057594077818641" author="35468159852@N01" authorname="Rev Dan Catt" datecreate="1141841470" permalink="http://www.flickr.com/photos/straup/109722179/#comment72057594077818641">
Umm, I'm not sure, can I get back to you on that one?</comment>
</comments>`
我想将通过调用该方法接收到的数据存储到一个变量 comments
中,我可以使用例如
I want to store the data received by calling that method into a variable comments
that I can access the data using, for example,
comments.all.first.author_name = "Rev Dan Catt"
或
comments.all.first.content = "Umm, I'm not sure, can I get back to..."
我该怎么做?请耐心等待(抱歉菜鸟问题)
How can I do this? Please be patient (sorry for the noob question)
推荐答案
include 'rubygems'
include 'httparty'
class Flickr
include HTTParty
base_uri 'api.flickr.com'
def self.getPhotos(uid)
return get('/services/rest/', :query => {
:method => 'flickr.people.getPublicPhotos',
:api_key => 'api key goes here',
:user_id => uid})
end
end
然后调用
Flickr.getPhotos('12345678')
这篇关于如何在 Ruby on Rails 中调用 API(例如 Flickr API)?新手问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!