我正在尝试创建一个表单,供用户使用他们的“手机刮刮卡”从我的Ruby on Rails网站上购买产品。
问题在于该服务仅提供PHP的模块代码。因此,我必须将其转换为Ruby才能放入我的网站。以下是我要转换为Ruby的代码:
$post_field = 'xyz=123&abc=456';
$api_url = "https://www.nganluong.vn/mobile_card.api.post.v2.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$api_url);
curl_setopt($ch, CURLOPT_ENCODING , 'UTF-8');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field);
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
我试图将它们转换为Ruby代码,但始终感到困惑。谁能帮助我将这些代码转换为有效的Ruby代码?提前致谢!
到目前为止,这是我的愚蠢代码:
RestClient.post($api_url, $post_field, "Content-Type" => "application/x-www-form-urlencoded")
基本上,我所需要的只是php curl代码的红宝石版本。我是新手,不是经验丰富的程序员,所以请帮忙。
尝试这样的事情:
require 'rest-client'
require 'rack'
post_query = 'xyz=123&abc=456'
api_url = "https://www.nganluong.vn/mobile_card.api.post.v2.php"
query_hash = Rack::Utils.parse_nested_query(post_query)
begin
response = RestClient.post api_url, :params => query_hash
print response.code
print response.body
rescue Exception => e
print e.message
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句