使用Ruby进行Facebook FQL查询

我正在尝试使用ruby对Facebook fql.query方法进行简单的GET,但没有成功。

url基本上是这样构建的:

https://api.facebook.com/method/fql.query?query=SELECT total_count FROM link_stat WHERE url = "http://twitter.com/"&format=json 

我在StackOverflow上的一些post中读到了关于如何发出这些请求的内容,但即便如此,我仍然会得到:

/usr/lib/ruby/1.8/net/http.rb:560:in’initialize’:getaddrinfo:名称或服务未知(SocketError)

在http_get函数的第一行。

 def http_get(domain,path,params) return Net::HTTP.get(domain, "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&'))) if not params.nil? return Net::HTTP.get(domain, path) end def getFacebookStats(url) params = { :query => 'SELECT total_count FROM link_stat WHERE url = "' + url + '"', :format => 'json' } http = http_get('https://api.facebook.com', '/method/fql.query', params) puts http end 

http调用接受主机 ,而不是URL:

 def http_get(domain,path,params) path = unless params.blank path + "?" + params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&') else path end request = Net::HTTP.get(domain, path) end def get_facebook_stats(url) params = { :query => 'SELECT total_count FROM link_stat WHERE url = "' + url + '"', :format => 'json' } http = http_get('api.facebook.com', '/method/fql.query', params) puts http end 

不要Ruby上的方法名称上使用驼峰大小写

如果要进行HTTPS呼叫,则必须使用其他呼叫:

 require 'net/http' require 'net/https' http = Net::HTTP.new('somehost.com', 443) http.use_ssl = true path = '/login.html' resp, data = http.get(path, nil)