dongwu4834 2018-05-15 11:17
浏览 77
已采纳

如何将PHP SoapClient请求示例转换为RoR?

I'd like to use some web service via its API. In documentation I found an example request written with PHP SoapClient. But I am using RoR and I have no PHP experience. Could someone tell me how should I write the same in RoR, or at least translate it to plain HTTP terminology?

<?php
  $soap = new SoapClient(“https://secure.przelewy24.pl/external/wsdl/service.php?wsdl”);
  $test = $soap->TestAccess(“9999”, “anuniquekeyretrievedfromprzelewy24”);
  if ($test)
    echo ‘Access granted’;
  else
    echo ‘Access denied’;
?> 

Edit: particularly I'd like to know what should I do with TestAccess method, because there's no methods in plain HTTP. Should I join this name with URL?

  • 写回答

2条回答 默认 最新

  • dongsu4345 2018-05-15 13:31
    关注

    To make your life easier, check out a gem that allows you to simplify SOAP access, like savon.

    Then the code could be translated as

    # create a client for the service
    client = Savon.client(wsdl: 'https://secure.przelewy24.pl/external/wsdl/service.php?wsdl')
    

    This will automatically parse the possible methods to client that are offered in the SOAP API (defined in the WSDL). To list the possible operations, type

    client.operations
    

    In your case this will list

    [:test_access, :trn_refund, :trn_by_session_id, :trn_full_by_session_id, :trn_list_by_date, :trn_list_by_batch, :trn_full_by_batch, :payment_methods, :currency_exchange, :refund_by_id, :trn_register, :trn_internal_register, :check_nip, :company_register, :company_update, :batch_list, :trn_dispatch, :charge_back, :trn_check_funds, :check_merchant_funds, :transfer_merchant_funds, :verify_transaction, :register_transaction, :deny_transaction, :batch_details]
    

    Then to call the method, do the following

    response = client.call(:test_access, message: { test_access_in: 9999 })
    response = client.call(:test_access, message: { 
       test_access_in: 9999 }
       test_access_out: "anuniquekeyretrievedfromprzelewy24" 
    )
    response.body
     => {:test_access_response=>{:return=>false}}
    

    this gets a result, but I have no idea what it means.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dqlxtv1452 2018-05-15 12:02
    关注

    I've included an entire controller method that we use in production as an example but essentially you want to pass in your xml/wsdl request as the body of the HTTP request and then parse the response as xml, or what we used below which is rexml for easier traversing of the returned doc.

    def get_chrome_styles
      require 'net/http'
      require 'net/https'
      require 'rexml/document'
      require 'rexml/formatters/pretty'
    
      xml = '<?xml version="1.0" encoding="UTF-8"?>
          <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:description7b.services.chrome.com">
            <soapenv:Header/>
            <soapenv:Body>
               <urn:StylesRequest modelId="' + params[:model_id] + '">
                  <urn:accountInfo number="[redacted]" secret="[redacted]" country="US" language="en" behalfOf="trace"/>
                  <!--Optional:-->
               </urn:StylesRequest>
            </soapenv:Body>
          </soapenv:Envelope>'
    
    
      base_url = 'http://services.chromedata.com/Description/7b?wsdl'
      uri = URI.parse( base_url )
      http = Net::HTTP.new(uri.host, uri.port)
    
      request = Net::HTTP::Post.new("/Description/7b?wsdl")
      request.add_field('Content-Type', 'text/xml; charset=utf-8')
      request.body = xml
      response = http.request( request )
    
      doc  = REXML::Document.new(response.body)
    
      options = []
      doc.get_elements('//style').each do |division|
        puts division
        options << { :id => division.attributes['id'], :val => division.text }
      end
    
      respond_to do |format|
        format.json { render :json => options.to_json }
      end
    end
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Qt 不小心删除了自带的类,该怎么办
  • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
  • ¥15 有没有哪位厉害的人可以用C#可视化呀
  • ¥15 可以帮我看看代码哪里错了吗
  • ¥15 设计一个成绩管理系统
  • ¥15 PCL注册的选点等函数如何取消注册
  • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
  • ¥15 Python爬虫程序
  • ¥15 crypto 这种的应该怎么找flag?
  • ¥15 代码已写好,求帮我指出错误,有偿!