【问题标题】:How to translate php curl_setopt to ruby equivalent?如何将 php curl_setopt 转换为 ruby​​ 等效项?
【发布时间】:2013-07-02 20:01:38
【问题描述】:

我需要一些帮助将 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;

到目前为止我已经完成的 ruby​​ 代码..

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

所以问题是:

  • 如何翻译#curl_setopt($ch,CURLOPT_POST, count($fieldsString));
  • 如何翻译#curl_setopt($ch,CURLOPT_HEADER, true);
  • easy.http_post 会执行我想要执行的操作吗?即发布带有给定标题选项等的参数...
  • 任何其他建议

谢谢

【问题讨论】:

    标签: php ruby curl translate


    【解决方案1】:

    如何翻译#curl_setopt($ch,CURLOPT_HEADER, true);

    像这样:

     easy.header_in_body = true
    

    如何翻译#curl_setopt($ch,CURLOPT_POST, 计数($fieldsString));

    php curl_setopt() 文档说:

    CURLOPT_HTTPGET 为真

    将 HTTP 请求方法重置为 GET。由于 GET 是默认的, 仅当请求方法已更改时才需要这样做。

    换句话说,如果你写:

    curl_setopt($ch,CURLOPT_POST, FALSE);
    

    请求默认为获取请求。所以...

    http_method = (postparams.length==0) ? 'get' : 'post'
    easy.http(http_method)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 2014-04-21
      • 2010-09-21
      • 1970-01-01
      相关资源
      最近更新 更多