首頁 > 後端開發 > php教程 > PHP to Ruby Code

PHP to Ruby Code

WBOY
發布: 2016-06-06 20:50:54
原創
888 人瀏覽過

和下面这段代码相同的ruby 怎么写呢?

public function makeRequest($url, $method, $postfields = NULL) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        if ('POST' === $method) {
            curl_setopt($ch, CURLOPT_POST, 1);
            if (!empty($postfields)) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
            }
        }
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_connectTimeOut);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeOut);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->_userAgent);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
登入後複製
登入後複製

THK

回复内容:

和下面这段代码相同的ruby 怎么写呢?

public function makeRequest($url, $method, $postfields = NULL) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        if ('POST' === $method) {
            curl_setopt($ch, CURLOPT_POST, 1);
            if (!empty($postfields)) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
            }
        }
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_connectTimeOut);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeOut);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->_userAgent);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
登入後複製
登入後複製

THK

  require 'uri'
  require 'net/http'
  require 'openssl'
  def makeRequest(url, method, postfields="", isHttps=false)
    httpHeader = {
	"Expect:"=>""
    }

    uri = URI(url)
    response = nil
    http = Net::HTTP.new(uri.host, uri.port)
    if isHttps
	http.use_ssl = true
	http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    http.start()    
    response = http.send_request(method, uri.path, postfields, httpHeader) 

    ret_code = response.code;
    ret_str = response.body;
    return ret_str;
  end
登入後複製

推荐一个不同编程语言之间转换的项目矩阵,楼主可以看看

http://langlangmatrix.com/

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板