我需要一些帮助将php脚本翻译成ruby
php脚本:
$apiUrl = 'http://someurl/' . $_POST['type'];
unset($_POST['type']);
$fields = $_POST;
$fields['ip'] = $_SERVER['REMOTE_ADDR'];
$fieldsString = http_build_query($fields);
$ch = curl_init();
////set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Api-Key: somekey'
));
curl_setopt($ch,CURLOPT_URL, $apiUrl);
curl_setopt($ch,CURLOPT_POST, count($fieldsString));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fieldsString);
echo "<pre>";
var_dump($fields);
////execute post
$result = curl_exec($ch);
////close connection
curl_close($ch);
exit;
到目前为止我做过的红宝石代码..
postparams = {
'sent_from' => 2,
'id' => @client.id,
...etc...
'type' => type,
'ip' => request.remote_ip
}
apiUrl = "http://someurl/#{type}"
fields = postparams
#$fieldsString = http_build_query($fields);
fieldsString = fields.to_query
#$ch = curl_init();
easy = Curl::Easy.new
#curl_setopt($ch,CURLOPT_HEADER, true);
# curl_setopt($ch, CURLOPT_HTTPHEADER, array(
# 'Api-Key: somekey'
# ));
easy.headers = ["Api-Key: somekey"]
#curl_setopt($ch,CURLOPT_URL, $apiUrl);
easy.url = apiUrl
#curl_setopt($ch,CURLOPT_POST, count($fieldsString));
#curl_setopt($ch,CURLOPT_POSTFIELDS, $fieldsString);
res = easy.http_post(apiUrl, fieldsString)
#$result = curl_exec($ch);
render :text => res.inspect
所以问题是:
谢谢
答案 0 :(得分:1)
如何翻译#curl_setopt($ ch,CURLOPT_HEADER,true);
像这样:
easy.header_in_body = true
我如何翻译#curl_setopt($ ch,CURLOPT_POST, 计数($ fieldsString));
php curl_setopt()文档说:
CURLOPT_HTTPGET TRUE
将HTTP请求方法重置为GET。由于 GET是默认, 只有在请求方法发生变化时才需要这样做。
换句话说,如果你写:
curl_setopt($ch,CURLOPT_POST, FALSE);
请求默认为get请求。所以......
http_method = (postparams.length==0) ? 'get' : 'post'
easy.http(http_method)