search

Home  >  Q&A  >  body text

PHP to Ruby Code

和下面这段代码相同的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

巴扎黑巴扎黑2902 days ago330

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-10 14:26:29

      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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-10 14:26:29

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

    http://langlangmatrix.com/

    reply
    0
  • Cancelreply