2011-10-14 33 views
0

我在PHP中拥有以下代码片段。我正在寻找将其转换为Ruby的最佳方式。我看过几种方法,包括open-uri和curb和wrapper-curb-fu库。 open-uri看起来不太好,但我真的很喜欢curb-fu的方法。但是,我有一种感觉,使用两个库是过度的,必须有一个更简单的方法来完成这段代码的工作。CURLing的最佳RoR方法

#Setup connection 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $resource_uri); 
    curl_setopt($curl, CURLOPT_HEADER, 0); 
    curl_setopt($curl, CURLOPT_USERPWD, $site_public_key . ":" . $site_private_key); 
    curl_setopt($curl, CURLOPT_TIMEOUT, 15); 
    curl_setopt($curl, CURLOPT_VERBOSE, 0); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1); 
    curl_setopt($curl, CURLOPT_FAILONERROR, 0); 

    #Send request 
    $result_json = curl_exec($curl) 
+0

请帮我解决这个问题http://stackoverflow.com/questions/ 16351700/curl-request-in-rails-using-rest-client-for-spellchecker –

回答

3

最好的选择是使用rest-client。它的API是真的很酷,重量轻:

result = RestClient::Request.new({:user => "username", :password => "password", 
         :method => :get, :url => "www.whatever.com"}).execute 

,或者如果您不需要身份验证,你可以简单地做:

result = RestClient.get("http://www.whatever.com") 
+0

请帮我解决这个问题.. http://stackoverflow.com/questions/16351700/curl-request-in-rails-using-其余客户端换拼写检查 –