最佳 CURLing RoR 方法

发布于 2024-12-09 16:41:56 字数 700 浏览 0 评论 0原文

我有以下 PHP 代码。我正在寻找将其转换为 Ruby 的最佳方法。我研究了几种方法,包括 open-uri 以及遏制和包装器遏制-fu 库。 open-uri 看起来不太好,但我真的很喜欢遏制-fu 方法。但是,我有一种感觉,使用两个库太过分了,必须有一种更简单的方法来完成这段代码正在做的事情。

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

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 approaches, 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-12-16 16:41:57

最好的选择是使用 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")

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文