开发者

Best RoR approach for CURLing

I have the following piece of code in PHP. I'm looking for the best way to convert it to Ruby. I've looked at a few ap开发者_开发百科proaches, including open-uri and the curb and wrapper curb-fu libraries. open-uri doesn't look very good, but I really like the curb-fu approach. But, I have a feeling using two libraries for this is overkill, there has to be a simpler way to accomplish what this piece of code is doing.

 #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)


Your best bet is to use rest-client. Its api is really cool and lightweight :

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

or if you don't need auth you can simply do :

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜