在 Rails 应用程序中编写类似 cURL 的函数

发布于 2024-09-25 12:54:58 字数 1933 浏览 7 评论 0原文

我正在尝试将此 PHP cURL 函数转换为与我的 Rails 应用程序一起使用。该代码来自短信支付网关,需要验证POST参数。因为我是一个 PHP 大菜鸟,所以我不知道如何处理这个问题。

$verify_url = 'http://smsgatewayadress';



    $fields = '';

    $d = array(

            'merchant_ID' => $_POST['merchant_ID'],

            'local_ID' => $_POST['local_ID'],

            'total' => $_POST['total'],

            'ipn_verify' => $_POST['ipn_verify'],

            'timeout' => 10, 
            );



    foreach ($d as $k => $v)

    {

        $fields .= $k . "=" . urlencode($v) . "&";

    }

    $fields = substr($fields, 0, strlen($fields)-1);



    $ch = curl_init($verify_url); //this initiates a HTTP connection to $verify_url, the connection headers will be stored in $ch 

    curl_setopt($ch, CURLOPT_POST, 1); //sets the delivery method as POST

    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); //The data that is being sent via POST. From what I can see the cURL lib sends them as a string that is built in the foreach loop above

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //This verifies if the target url sends a redirect header and if it does cURL follows that link

    curl_setopt($ch, CURLOPT_HEADER, 0); //This ignores the headers from the answer

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //This specifies that the curl_exec function below must return the result to the accesed URL

    $result = curl_exec($ch); //It ransfers the data via POST to the URL, it gets read and returns the result



    if ($result == true)

    {

        //confirmed

        $can_download = true;

    }

    else

    {

        //failed
        $can_download = false;

    }

}



if (strpos($_SERVER['REQUEST_URI'], 'ipn.php'))

    echo $can_download ? '1' : '0'; //we tell the sms sever that we processed the request

我用 google 搜索了 Rails 中的 cURL lib 对应项,找到了大量选项,但没有一个选项是我能像该脚本那样理解和使用的。

如果有人能帮助我将此脚本从 php 转换为 ruby​​,我将不胜感激。

I'm trying to convert this PHP cURL function to work with my rails app. The piece of code is from an SMS payment gateway that needs to verify the POST paramters. Since I'm a big PHP noob I have no idea how to handle this problem.

$verify_url = 'http://smsgatewayadress';



    $fields = '';

    $d = array(

            'merchant_ID' => $_POST['merchant_ID'],

            'local_ID' => $_POST['local_ID'],

            'total' => $_POST['total'],

            'ipn_verify' => $_POST['ipn_verify'],

            'timeout' => 10, 
            );



    foreach ($d as $k => $v)

    {

        $fields .= $k . "=" . urlencode($v) . "&";

    }

    $fields = substr($fields, 0, strlen($fields)-1);



    $ch = curl_init($verify_url); //this initiates a HTTP connection to $verify_url, the connection headers will be stored in $ch 

    curl_setopt($ch, CURLOPT_POST, 1); //sets the delivery method as POST

    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); //The data that is being sent via POST. From what I can see the cURL lib sends them as a string that is built in the foreach loop above

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //This verifies if the target url sends a redirect header and if it does cURL follows that link

    curl_setopt($ch, CURLOPT_HEADER, 0); //This ignores the headers from the answer

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //This specifies that the curl_exec function below must return the result to the accesed URL

    $result = curl_exec($ch); //It ransfers the data via POST to the URL, it gets read and returns the result



    if ($result == true)

    {

        //confirmed

        $can_download = true;

    }

    else

    {

        //failed
        $can_download = false;

    }

}



if (strpos($_SERVER['REQUEST_URI'], 'ipn.php'))

    echo $can_download ? '1' : '0'; //we tell the sms sever that we processed the request

I've googled a cURL lib counterpart in Rails and found a ton of options but none that I could understand and use in the same way this script does.

If anyone could give me a hand with converting this script from php to ruby it would be greatly appreciated.

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

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

发布评论

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

评论(1

没有你我更好 2024-10-02 12:54:58

最直接的方法可能是使用 Ruby curb 库,这是最cURL 的简单包装。 Curl::Easy 中的许多选项直接映射到您此处的内容。一个基础可能是:

url = "http://smsgatewayadress/"
Curl::Easy.http_post(url,
  Curl::PostField.content('merchant_ID', params[:merchant_ID]),
  # ...
)

The most direct approach might be to use the Ruby curb library, which is the most straightforward wrapper for cURL. A lot of the options in Curl::Easy map directly to what you have here. A basis might be:

url = "http://smsgatewayadress/"
Curl::Easy.http_post(url,
  Curl::PostField.content('merchant_ID', params[:merchant_ID]),
  # ...
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文